summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmer Katz <omer.drow@gmail.com>2020-07-23 16:29:29 +0300
committerOmer Katz <omer.drow@gmail.com>2020-07-23 16:29:29 +0300
commit1916b815f88551138ff26eda3e4af4c519c893e3 (patch)
tree761a6c3c31e3bc28edbdcfb2a7470ece2f3f785b
parent24ed1179b58d60c2470346e349dbcdabc173f6f3 (diff)
downloadkombu-1916b815f88551138ff26eda3e4af4c519c893e3.tar.gz
Remove five usage from kombu/utils.
-rw-r--r--kombu/utils/compat.py7
-rw-r--r--kombu/utils/debug.py4
-rw-r--r--kombu/utils/encoding.py104
-rw-r--r--kombu/utils/functional.py47
-rw-r--r--kombu/utils/imports.py4
-rw-r--r--kombu/utils/json.py10
-rw-r--r--kombu/utils/limits.py3
-rw-r--r--kombu/utils/scheduling.py3
-rw-r--r--kombu/utils/text.py3
-rw-r--r--kombu/utils/url.py6
10 files changed, 55 insertions, 136 deletions
diff --git a/kombu/utils/compat.py b/kombu/utils/compat.py
index f5ee70eb..c8277695 100644
--- a/kombu/utils/compat.py
+++ b/kombu/utils/compat.py
@@ -7,12 +7,9 @@ from functools import wraps
from contextlib import contextmanager
-try:
- from importlib import metadata as importlib_metadata
-except ImportError:
- import importlib_metadata
+from importlib import metadata as importlib_metadata
-from kombu.five import reraise
+from kombu.exceptions import reraise
try:
from io import UnsupportedOperation
diff --git a/kombu/utils/debug.py b/kombu/utils/debug.py
index 2ddaa524..acc2d60b 100644
--- a/kombu/utils/debug.py
+++ b/kombu/utils/debug.py
@@ -4,7 +4,6 @@ import logging
from vine.utils import wraps
-from kombu.five import items, python_2_unicode_compatible
from kombu.log import get_logger
__all__ = ('setup_logging', 'Logwrapped')
@@ -19,7 +18,6 @@ def setup_logging(loglevel=logging.DEBUG, loggers=None):
logger.setLevel(loglevel)
-@python_2_unicode_compatible
class Logwrapped:
"""Wrap all object methods, to log on call."""
@@ -48,7 +46,7 @@ class Logwrapped:
if args:
info += ', '
info += ', '.join(f'{key}={value!r}'
- for key, value in items(kwargs))
+ for key, value in kwargs.items())
info += ')'
self.logger.debug(info)
return meth(*args, **kwargs)
diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py
index 5d3e1efb..d18545f9 100644
--- a/kombu/utils/encoding.py
+++ b/kombu/utils/encoding.py
@@ -8,9 +8,6 @@ applications without crashing from the infamous
import sys
import traceback
-from kombu.five import PY3, text_t
-
-
#: safe_str takes encoding from this file by default.
#: :func:`set_default_encoding_file` can used to set the
#: default output file.
@@ -28,7 +25,7 @@ def get_default_encoding_file():
return default_encoding_file
-if sys.platform.startswith('java'): # pragma: no cover
+if sys.platform.startswith('java'): # pragma: no cover
def default_encoding(file=None):
"""Get default encoding."""
@@ -40,59 +37,39 @@ else:
file = file or get_default_encoding_file()
return getattr(file, 'encoding', None) or sys.getfilesystemencoding()
-if PY3: # pragma: no cover
- def str_to_bytes(s):
- """Convert str to bytes."""
- if isinstance(s, str):
- return s.encode()
- return s
+def str_to_bytes(s):
+ """Convert str to bytes."""
+ if isinstance(s, str):
+ return s.encode()
+ return s
- def bytes_to_str(s):
- """Convert bytes to str."""
- if isinstance(s, bytes):
- return s.decode(errors='replace')
- return s
- def from_utf8(s, *args, **kwargs):
- """Get str from utf-8 encoding."""
- return s
+def bytes_to_str(s):
+ """Convert bytes to str."""
+ if isinstance(s, bytes):
+ return s.decode(errors='replace')
+ return s
- def ensure_bytes(s):
- """Ensure s is bytes, not str."""
- if not isinstance(s, bytes):
- return str_to_bytes(s)
- return s
- def default_encode(obj):
- """Encode using default encoding."""
- return obj
+def from_utf8(s, *args, **kwargs):
+ """Get str from utf-8 encoding."""
+ return s
- str_t = str
-else:
+def ensure_bytes(s):
+ """Ensure s is bytes, not str."""
+ if not isinstance(s, bytes):
+ return str_to_bytes(s)
+ return s
- def str_to_bytes(s): # noqa
- """Convert str to bytes."""
- if isinstance(s, unicode):
- return s.encode()
- return s
- def bytes_to_str(s): # noqa
- """Convert bytes to str."""
- return s
+def default_encode(obj):
+ """Encode using default encoding."""
+ return obj
- def from_utf8(s, *args, **kwargs): # noqa
- """Convert utf-8 to ASCII."""
- return s.encode('utf-8', *args, **kwargs)
-
- def default_encode(obj, file=None): # noqa
- """Get default encoding."""
- return unicode(obj, default_encoding(file))
-
- str_t = unicode
- ensure_bytes = str_to_bytes
+str_t = str
try:
bytes_t = bytes
@@ -103,38 +80,19 @@ except NameError: # pragma: no cover
def safe_str(s, errors='replace'):
"""Safe form of str(), void of unicode errors."""
s = bytes_to_str(s)
- if not isinstance(s, (text_t, bytes)):
+ if not isinstance(s, (str, bytes)):
return safe_repr(s, errors)
return _safe_str(s, errors)
-if PY3: # pragma: no cover
-
- def _safe_str(s, errors='replace', file=None):
- if isinstance(s, str):
- return s
- try:
- return str(s)
- except Exception as exc:
- return '<Unrepresentable {!r}: {!r} {!r}>'.format(
- type(s), exc, '\n'.join(traceback.format_stack()))
-else:
- def _ensure_str(s, encoding, errors):
- if isinstance(s, bytes):
- return s.decode(encoding, errors)
+def _safe_str(s, errors='replace', file=None):
+ if isinstance(s, str):
return s
-
-
- def _safe_str(s, errors='replace', file=None): # noqa
- encoding = default_encoding(file)
- try:
- if isinstance(s, unicode):
- return _ensure_str(s.encode(encoding, errors),
- encoding, errors)
- return unicode(s, encoding, errors)
- except Exception as exc:
- return '<Unrepresentable {!r}: {!r} {!r}>'.format(
- type(s), exc, '\n'.join(traceback.format_stack()))
+ try:
+ return str(s)
+ except Exception as exc:
+ return '<Unrepresentable {!r}: {!r} {!r}>'.format(
+ type(s), exc, '\n'.join(traceback.format_stack()))
def safe_repr(o, errors='replace'):
diff --git a/kombu/utils/functional.py b/kombu/utils/functional.py
index 52e9aa1e..30b04774 100644
--- a/kombu/utils/functional.py
+++ b/kombu/utils/functional.py
@@ -1,26 +1,17 @@
"""Functional Utilities."""
+from collections import OrderedDict, UserDict
+from collections.abc import Iterable, Mapping
import random
import sys
import threading
import inspect
-from collections import OrderedDict
-
-try:
- from collections.abc import Iterable, Mapping
-except ImportError:
- from collections import Iterable, Mapping
-
from itertools import count, repeat
from time import sleep, time
from vine.utils import wraps
-from kombu.five import (
- UserDict, items, keys, python_2_unicode_compatible, string_t, PY3,
-)
-
from .encoding import safe_repr as _safe_repr
__all__ = (
@@ -31,7 +22,6 @@ __all__ = (
KEYWORD_MARK = object()
-@python_2_unicode_compatible
class ChannelPromise:
def __init__(self, contract):
@@ -116,7 +106,7 @@ class LRUCache(UserDict):
def _iterate_keys(self):
# userdict.keys in py3k calls __getitem__
with self.mutex:
- return keys(self.data)
+ return self.data.keys()
iterkeys = _iterate_keys
def incr(self, key, delta=1):
@@ -189,7 +179,6 @@ def memoize(maxsize=None, keyfun=None, Cache=LRUCache):
return _memoize
-@python_2_unicode_compatible
class lazy:
"""Holds lazy evaluation.
@@ -231,13 +220,6 @@ class lazy:
return (self.__class__, (self._fun,), {'_args': self._args,
'_kwargs': self._kwargs})
- if sys.version_info[0] < 3:
-
- def __cmp__(self, rhs):
- if isinstance(rhs, self.__class__):
- return -cmp(rhs, self())
- return cmp(self(), rhs)
-
def maybe_evaluate(value):
"""Evaluate value only if value is a :class:`lazy` instance."""
@@ -246,7 +228,7 @@ def maybe_evaluate(value):
return value
-def is_list(obj, scalars=(Mapping, string_t), iters=(Iterable,)):
+def is_list(obj, scalars=(Mapping, str), iters=(Iterable,)):
"""Return true if the object is iterable.
Note:
@@ -255,7 +237,7 @@ def is_list(obj, scalars=(Mapping, string_t), iters=(Iterable,)):
return isinstance(obj, iters) and not isinstance(obj, scalars or ())
-def maybe_list(obj, scalars=(Mapping, string_t)):
+def maybe_list(obj, scalars=(Mapping, str)):
"""Return list of one element if ``l`` is a scalar."""
return obj if obj is None or is_list(obj, scalars) else [obj]
@@ -263,7 +245,7 @@ def maybe_list(obj, scalars=(Mapping, string_t)):
def dictfilter(d=None, **kw):
"""Remove all keys from dict ``d`` whose value is :const:`None`."""
d = kw if d is None else (dict(d, **kw) if kw else d)
- return {k: v for k, v in items(d) if v is not None}
+ return {k: v for k, v in d.items() if v is not None}
def shufflecycle(it):
@@ -360,7 +342,7 @@ def retry_over_time(fun, catch, args=None, kwargs=None, errback=None,
def reprkwargs(kwargs, sep=', ', fmt='{0}={1}'):
- return sep.join(fmt.format(k, _safe_repr(v)) for k, v in items(kwargs))
+ return sep.join(fmt.format(k, _safe_repr(v)) for k, v in kwargs.items())
def reprcall(name, args=(), kwargs=None, sep=', '):
@@ -373,16 +355,11 @@ def reprcall(name, args=(), kwargs=None, sep=', '):
def accepts_argument(func, argument_name):
- if PY3:
- argument_spec = inspect.getfullargspec(func)
- return (
- argument_name in argument_spec.args or
- argument_name in argument_spec.kwonlyargs
- )
-
- argument_spec = inspect.getargspec(func)
- argument_names = argument_spec.args
- return argument_name in argument_names
+ argument_spec = inspect.getfullargspec(func)
+ return (
+ argument_name in argument_spec.args or
+ argument_name in argument_spec.kwonlyargs
+ )
# Compat names (before kombu 3.0)
diff --git a/kombu/utils/imports.py b/kombu/utils/imports.py
index 51024739..fd4482a8 100644
--- a/kombu/utils/imports.py
+++ b/kombu/utils/imports.py
@@ -3,7 +3,7 @@
import importlib
import sys
-from kombu.five import reraise, string_t
+from kombu.exceptions import reraise
def symbol_by_name(name, aliases=None, imp=None, package=None,
@@ -43,7 +43,7 @@ def symbol_by_name(name, aliases=None, imp=None, package=None,
if imp is None:
imp = importlib.import_module
- if not isinstance(name, string_t):
+ if not isinstance(name, str):
return name # already a class
name = aliases.get(name) or name
diff --git a/kombu/utils/json.py b/kombu/utils/json.py
index 4a0f1114..8dad2600 100644
--- a/kombu/utils/json.py
+++ b/kombu/utils/json.py
@@ -5,8 +5,6 @@ import decimal
import json as stdjson
import uuid
-from kombu.five import PY3, buffer_t, text_t, bytes_t
-
try:
from django.utils.functional import Promise as DjangoPromise
except ImportError: # pragma: no cover
@@ -38,7 +36,7 @@ class JSONEncoder(_encoder_cls):
textual=(decimal.Decimal, uuid.UUID, DjangoPromise),
isinstance=isinstance,
datetime=datetime.datetime,
- text_t=text_t):
+ text_t=str):
reducer = getattr(o, '__json__', None)
if reducer is not None:
return reducer()
@@ -68,7 +66,7 @@ def dumps(s, _dumps=json.dumps, cls=None, default_kwargs=None, **kwargs):
**dict(default_kwargs, **kwargs))
-def loads(s, _loads=json.loads, decode_bytes=PY3):
+def loads(s, _loads=json.loads, decode_bytes=True):
"""Deserialize json from string."""
# None of the json implementations supports decoding from
# a buffer/memoryview, or even reading from a stream
@@ -80,10 +78,8 @@ def loads(s, _loads=json.loads, decode_bytes=PY3):
s = s.tobytes().decode('utf-8')
elif isinstance(s, bytearray):
s = s.decode('utf-8')
- elif decode_bytes and isinstance(s, bytes_t):
+ elif decode_bytes and isinstance(s, bytes):
s = s.decode('utf-8')
- elif isinstance(s, buffer_t):
- s = text_t(s) # ... awwwwwww :(
try:
return _loads(s)
diff --git a/kombu/utils/limits.py b/kombu/utils/limits.py
index a18149b1..d82884f5 100644
--- a/kombu/utils/limits.py
+++ b/kombu/utils/limits.py
@@ -1,8 +1,7 @@
"""Token bucket implementation for rate limiting."""
from collections import deque
-
-from kombu.five import monotonic
+from time import monotonic
__all__ = ('TokenBucket',)
diff --git a/kombu/utils/scheduling.py b/kombu/utils/scheduling.py
index 22b6733b..be663e2e 100644
--- a/kombu/utils/scheduling.py
+++ b/kombu/utils/scheduling.py
@@ -2,8 +2,6 @@
from itertools import count
-from kombu.five import python_2_unicode_compatible
-
from .imports import symbol_by_name
__all__ = (
@@ -17,7 +15,6 @@ CYCLE_ALIASES = {
}
-@python_2_unicode_compatible
class FairCycle:
"""Cycle between resources.
diff --git a/kombu/utils/text.py b/kombu/utils/text.py
index 30eb5497..1d5fb9de 100644
--- a/kombu/utils/text.py
+++ b/kombu/utils/text.py
@@ -5,7 +5,6 @@
from difflib import SequenceMatcher
from kombu import version_info_t
-from kombu.five import string_t
def escape_regex(p, white=''):
@@ -46,7 +45,7 @@ def version_string_as_tuple(s):
"""Convert version string to version info tuple."""
v = _unpack_version(*s.split('.'))
# X.Y.3a1 -> (X, Y, 3, 'a1')
- if isinstance(v.micro, string_t):
+ if isinstance(v.micro, str):
v = version_info_t(v.major, v.minor, *_splitmicro(*v[2:]))
# X.Y.3a1-40 -> (X, Y, 3, 'a1', '40')
if not v.serial and v.releaselevel and '-' in v.releaselevel:
diff --git a/kombu/utils/url.py b/kombu/utils/url.py
index 17c8a7e8..ec2fb3e8 100644
--- a/kombu/utils/url.py
+++ b/kombu/utils/url.py
@@ -20,12 +20,10 @@ try:
except ImportError: # pragma: no cover
ssl_available = False
-from kombu.five import bytes_if_py2, string_t
-
from .compat import NamedTuple
from ..log import get_logger
-safequote = partial(quote, safe=bytes_if_py2(''))
+safequote = partial(quote, safe='')
logger = get_logger(__name__)
@@ -114,7 +112,7 @@ def sanitize_url(url, mask='**'):
def maybe_sanitize_url(url, mask='**'):
# type: (Any, str) -> Any
"""Sanitize url, or do nothing if url undefined."""
- if isinstance(url, string_t) and '://' in url:
+ if isinstance(url, str) and '://' in url:
return sanitize_url(url, mask)
return url