From 04a2683b2d2ea6a577524c3eb2ff7842e88c7356 Mon Sep 17 00:00:00 2001 From: xuanyandong Date: Thu, 8 Oct 2020 09:28:36 +0800 Subject: [PATCH] Remove six in files under cinder/* Replace the following items with Python 3 style code. - six.add_metaclass - six.PY2 - six.text_type - six.string_types - six.wraps Change-Id: I393ee1f1627a993021af395c02bb936a7a476aec Implements: blueprint six-removal --- cinder/context.py | 3 +-- cinder/exception.py | 5 ++--- cinder/rpc.py | 5 +++-- cinder/ssh_utils.py | 3 +-- cinder/utils.py | 27 ++++++++++----------------- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/cinder/context.py b/cinder/context.py index 31196263e24..9bd33dbe2fe 100644 --- a/cinder/context.py +++ b/cinder/context.py @@ -26,7 +26,6 @@ from oslo_context import context from oslo_db.sqlalchemy import enginefacade from oslo_log import log as logging from oslo_utils import timeutils -import six from cinder import exception from cinder.i18n import _ @@ -105,7 +104,7 @@ class RequestContext(context.RequestContext): self.remote_address = remote_address if not timestamp: timestamp = timeutils.utcnow() - elif isinstance(timestamp, six.string_types): + elif isinstance(timestamp, str): timestamp = timeutils.parse_isotime(timestamp) self.timestamp = timestamp self.quota_class = quota_class diff --git a/cinder/exception.py b/cinder/exception.py index b040166c9b3..2efa7f761dd 100644 --- a/cinder/exception.py +++ b/cinder/exception.py @@ -24,7 +24,6 @@ SHOULD include dedicated exception logging. from oslo_log import log as logging from oslo_versionedobjects import exception as obj_exc -import six import webob.exc from webob.util import status_generic_reasons from webob.util import status_reasons @@ -85,7 +84,7 @@ class CinderException(Exception): # NOTE(tommylikehu): If this is a cinder exception it will # return the msg object, so we won't be preventing # translations. - self.kwargs[k] = six.text_type(v) + self.kwargs[k] = str(v) if self._should_format(): try: @@ -100,7 +99,7 @@ class CinderException(Exception): # NOTE(tommylikehu): If this is a cinder exception it will # return the msg object, so we won't be preventing # translations. - message = six.text_type(message) + message = str(message) # NOTE(luisg): We put the actual message in 'msg' so that we can access # it, because if we try to access the message via 'message' it will be diff --git a/cinder/rpc.py b/cinder/rpc.py index c7609e3dc8b..d1f485417e7 100644 --- a/cinder/rpc.py +++ b/cinder/rpc.py @@ -25,13 +25,14 @@ __all__ = [ 'get_notifier', ] +import functools + from oslo_config import cfg from oslo_log import log as logging import oslo_messaging as messaging from oslo_messaging.rpc import dispatcher from oslo_utils import importutils profiler = importutils.try_import('osprofiler.profiler') -import six import cinder.context import cinder.exception @@ -177,7 +178,7 @@ def assert_min_rpc_version(min_ver, exc=None): exc = cinder.exception.ServiceTooOld def decorator(f): - @six.wraps(f) + @functools.wraps(f) def _wrapper(self, *args, **kwargs): if not self.client.can_send_version(min_ver): msg = _('One of %(binary)s services is too old to accept ' diff --git a/cinder/ssh_utils.py b/cinder/ssh_utils.py index ed389ae8622..6bd3fb7319d 100644 --- a/cinder/ssh_utils.py +++ b/cinder/ssh_utils.py @@ -25,7 +25,6 @@ from oslo_config import cfg from oslo_log import log as logging from oslo_utils import excutils import paramiko -import six from cinder import exception from cinder.i18n import _ @@ -159,7 +158,7 @@ class SSHPool(pools.Pool): transport.set_keepalive(self.conn_timeout) return ssh except Exception as e: - msg = _("Error connecting via ssh: %s") % six.text_type(e) + msg = _("Error connecting via ssh: %s") % str(e) LOG.error(msg) raise paramiko.SSHException(msg) diff --git a/cinder/utils.py b/cinder/utils.py index ee81b5b4a93..ce6de66680b 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -46,12 +46,10 @@ from oslo_concurrency import lockutils from oslo_concurrency import processutils from oslo_config import cfg from oslo_log import log as logging -from oslo_utils import encodeutils from oslo_utils import excutils from oslo_utils import importutils from oslo_utils import strutils from oslo_utils import timeutils -import six import tenacity from cinder import exception @@ -309,7 +307,7 @@ def monkey_patch(): if isinstance(module_data[key], pyclbr.Class): clz = importutils.import_class("%s.%s" % (module, key)) # On Python 3, unbound methods are regular functions - predicate = inspect.isfunction if six.PY3 else inspect.ismethod + predicate = inspect.isfunction for method, func in inspect.getmembers(clz, predicate): setattr( clz, method, @@ -415,8 +413,7 @@ def tempdir(**kwargs): try: shutil.rmtree(tmpdir) except OSError as e: - LOG.debug('Could not remove tmpdir: %s', - six.text_type(e)) + LOG.debug('Could not remove tmpdir: %s', str(e)) def get_root_helper(): @@ -670,7 +667,7 @@ def retry(exceptions, interval=1, retries=3, backoff_rate=2, def _decorator(f): - @six.wraps(f) + @functools.wraps(f) def _wrapper(*args, **kwargs): r = tenacity.Retrying( sleep=tenacity.nap.sleep, @@ -696,13 +693,10 @@ def convert_str(text): encode Unicode using encodeutils.safe_encode() * convert to Unicode on Python 3: decode bytes from UTF-8 """ - if six.PY2: - return encodeutils.to_utf8(text) + if isinstance(text, bytes): + return text.decode('utf-8') else: - if isinstance(text, bytes): - return text.decode('utf-8') - else: - return text + return text def trace_method(f): @@ -778,7 +772,7 @@ def trace(*dec_args, **dec_kwargs): logger.debug('==> %(func)s: call %(all_args)r', {'func': func_name, 'all_args': strutils.mask_password( - six.text_type(all_args))}) + str(all_args))}) start_time = time.time() * 1000 try: @@ -794,7 +788,7 @@ def trace(*dec_args, **dec_kwargs): if isinstance(result, dict): mask_result = strutils.mask_dict_password(result) - elif isinstance(result, six.string_types): + elif isinstance(result, str): mask_result = strutils.mask_password(result) else: mask_result = result @@ -822,8 +816,7 @@ class TraceWrapperMetaclass(type): decorated with the trace_method decorator. To use the metaclass you define a class like so: - @six.add_metaclass(utils.TraceWrapperMetaclass) - class MyClass(object): + class MyClass(object, metaclass=utils.TraceWrapperMetaclass): """ def __new__(meta, classname, bases, classDict): newClassDict = {} @@ -877,7 +870,7 @@ def build_or_str(elements, str_format=None): if not elements: return '' - if not isinstance(elements, six.string_types): + if not isinstance(elements, str): elements = _(' or ').join(elements) if str_format: