Jelajahi Sumber

Py3 tests and flakes

Ask Solem 11 tahun lalu
induk
melakukan
0dbc9b8439

+ 2 - 1
celery/backends/cache.py

@@ -45,7 +45,8 @@ def import_best_memcache():
                 import memcache  # noqa
             except ImportError:
                 raise ImproperlyConfigured(REQUIRES_BACKEND)
-            memcache_key_t = bytes_to_str if PY3 else ensure_bytes
+        if PY3:
+            memcache_key_t = bytes_to_str
         _imp[0] = (is_pylibmc, memcache, memcache_key_t)
     return _imp[0]
 

+ 1 - 8
celery/bin/amqp.py

@@ -67,14 +67,7 @@ class Spec(object):
         self.returns = kwargs.get('returns')
 
     def coerce(self, index, value):
-        """Coerce value for argument at index.
-
-        E.g. if :attr:`args` is `[('is_active', bool)]`:
-
-            >>> obj.coerce(0, 'False')
-            False
-
-        """
+        """Coerce value for argument at index."""
         arg_info = self.args[index]
         arg_type = arg_info[1]
         # Might be a custom way to coerce the string value,

+ 4 - 4
celery/five.py

@@ -92,11 +92,11 @@ else:
     from itertools import imap as map, izip_longest as zip_longest  # noqa
     string = unicode                # noqa
     string_t = basestring           # noqa
-    text_t = unicode
+    text_t = unicode                # noqa
     long_t = long                   # noqa
-    range = xrange
-    int_types = (int, long)
-    _byte_t = (str, bytes)
+    range = xrange                  # noqa
+    int_types = (int, long)         # noqa
+    _byte_t = (str, bytes)          # noqa
 
     open_fqdn = '__builtin__.open'
 

+ 3 - 3
celery/local.py

@@ -15,7 +15,7 @@ from __future__ import absolute_import
 import importlib
 import sys
 
-from .five import long_t, string
+from .five import string
 
 __all__ = ['Proxy', 'PromiseProxy', 'try_import', 'maybe_evaluate']
 
@@ -200,8 +200,8 @@ class Proxy(object):
     __reduce__ = lambda x: x._get_current_object().__reduce__()
 
     if not PY3:
-        __cmp__ = lambda x, o: cmp(x._get_current_object(), o)
-        __long__ = lambda x: long_t(x._get_current_object())
+        __cmp__ = lambda x, o: cmp(x._get_current_object(), o)  # noqa
+        __long__ = lambda x: long(x._get_current_object())      # noqa
 
 
 class PromiseProxy(Proxy):

+ 1 - 1
celery/task/http.py

@@ -46,7 +46,7 @@ else:
         keys/values encoded."""
         return dict(
             (k.encode('utf-8'),
-             v.encode('utf-8') if isinstance(v, unicode) else v)
+             v.encode('utf-8') if isinstance(v, unicode) else v)  # noqa
             for k, v in tup)
 
 

+ 10 - 3
celery/tests/backends/test_cache.py

@@ -18,6 +18,8 @@ from celery.tests.case import (
     AppCase, Mock, mask_modules, patch, reset_modules,
 )
 
+PY3 = sys.version_info[0] == 3
+
 
 class SomeClass(object):
 
@@ -122,10 +124,15 @@ class MyMemcachedStringEncodingError(Exception):
 class MemcachedClient(DummyClient):
 
     def set(self, key, value, *args, **kwargs):
-        if isinstance(key, text_t):
+        if PY3:
+            key_t, must_be, not_be, cod = bytes, 'string', 'bytes', 'decode'
+        else:
+            key_t, must_be, not_be, cod = text_t, 'bytes', 'string', 'encode'
+        if isinstance(key, key_t):
             raise MyMemcachedStringEncodingError(
-                'Keys must be bytes, not string.  Convert your '
-                'strings using mystring.encode(charset)!')
+                'Keys must be {0}, not {1}.  Convert your '
+                'strings using mystring.{2}(charset)!'.format(
+                    must_be, not_be, cod))
         return super(MemcachedClient, self).set(key, value, *args, **kwargs)
 
 

+ 4 - 1
celery/tests/utils/test_local.py

@@ -11,6 +11,8 @@ from celery.local import (
 )
 from celery.tests.case import Case, Mock
 
+PY3 = sys.version_info[0] == 3
+
 
 class test_try_import(Case):
 
@@ -258,7 +260,8 @@ class test_Proxy(Case):
         x = Proxy(lambda: 10)
         self.assertEqual(type(x.__float__()), float)
         self.assertEqual(type(x.__int__()), int)
-        self.assertEqual(type(x.__long__()), long_t)
+        if not PY3:
+            self.assertEqual(type(x.__long__()), long_t)
         self.assertTrue(hex(x))
         self.assertTrue(oct(x))
 

+ 10 - 6
celery/utils/dispatch/saferef.py

@@ -7,11 +7,14 @@ aren't handled by the core weakref module).
 """
 from __future__ import absolute_import
 
-import weakref
+import sys
 import traceback
+import weakref
 
 __all__ = ['safe_ref']
 
+PY3 = sys.version_info[0] == 3
+
 
 def safe_ref(target, on_delete=None):  # pragma: no cover
     """Return a *safe* weak reference to a callable target
@@ -178,11 +181,12 @@ class BoundMethodWeakref(object):  # pragma: no cover
         return self() is not None
     __nonzero__ = __bool__  # py2
 
-    def __cmp__(self, other):
-        """Compare with another reference"""
-        if not isinstance(other, self.__class__):
-            return cmp(self.__class__, type(other))
-        return cmp(self.key, other.key)
+    if not PY3:
+        def __cmp__(self, other):
+            """Compare with another reference"""
+            if not isinstance(other, self.__class__):
+                return cmp(self.__class__, type(other))  # noqa
+            return cmp(self.key, other.key)              # noqa
 
     def __call__(self):
         """Return a strong reference to the bound method