drop use of six

Change-Id: I0ad972f48ee6bc331b96aa67bd7e69c97fa8e2c3
This commit is contained in:
Hervé Beraud 2020-03-02 15:54:14 +01:00
parent 26abff6f54
commit 55ef517065
11 changed files with 75 additions and 154 deletions

View File

@ -54,7 +54,6 @@ reno==2.7.0
requests==2.18.4
requestsexceptions==1.4.0
rfc3986==1.1.0
six==1.11.0
smmap2==2.0.3
snowballstemmer==1.2.1
Sphinx==1.8.0

View File

@ -21,7 +21,6 @@ import time
from oslo_serialization import jsonutils
from oslo_utils import importutils
import six
from oslo_log import log
@ -180,8 +179,8 @@ def console_format(prefix, locator, record, loggers=[], levels=[],
# Thrown when a non-string format-specifier can't be filled in.
# Dict comprehension cleans up the output
yield warn('Missing non-string placeholder in record',
{str(k): str(v) if isinstance(v, six.string_types) else v
for k, v in six.iteritems(record)})
{str(k): str(v) if isinstance(v, str) else v
for k, v in record.items()})
return
locator = ''

View File

@ -13,6 +13,7 @@
import datetime
import debtcollector
import functools
import io
import itertools
import logging
import logging.config
@ -23,16 +24,11 @@ import sys
import traceback
from dateutil import tz
import six
from six import moves
from oslo_context import context as context_utils
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
if six.PY3:
from functools import reduce
def _dictify_context(context):
if getattr(context, 'get_logging_values', None):
@ -85,10 +81,10 @@ def _update_record_with_context(record):
def _ensure_unicode(msg):
"""Do our best to turn the input argument into a unicode object.
"""
if isinstance(msg, six.text_type):
if isinstance(msg, str):
return msg
if not isinstance(msg, six.binary_type):
return six.text_type(msg)
if not isinstance(msg, bytes):
return str(msg)
return encodeutils.safe_decode(
msg,
incoming='utf-8',
@ -157,7 +153,7 @@ def _get_error_summary(record):
error_summary = error_summary.split('\n', 1)[0]
except TypeError as type_err:
# Work around https://bugs.python.org/issue28603
error_summary = "<exception with %s>" % six.text_type(type_err)
error_summary = "<exception with %s>" % str(type_err)
finally:
# Remove the local reference to the exception and
# traceback to avoid a memory leak through the frame
@ -201,10 +197,10 @@ class JSONFormatter(logging.Formatter):
lines = traceback.format_exception(*ei)
except TypeError as type_error:
# Work around https://bugs.python.org/issue28603
msg = six.text_type(type_error)
msg = str(type_error)
lines = ['<Unprintable exception due to %s>\n' % msg]
if strip_newlines:
lines = [moves.filter(
lines = [filter(
lambda x: x,
line.rstrip().splitlines()) for line in lines]
lines = list(itertools.chain(*lines))
@ -300,11 +296,12 @@ class FluentFormatter(logging.Formatter):
lines = traceback.format_exception(*exc_info)
except TypeError as type_error:
# Work around https://bugs.python.org/issue28603
msg = six.text_type(type_error)
msg = str(type_error)
lines = ['<Unprintable exception due to %s>\n' % msg]
if strip_newlines:
lines = reduce(lambda a, line: a + line.rstrip().splitlines(),
lines, [])
lines = functools.reduce(lambda a,
line: a + line.rstrip().splitlines(),
lines, [])
return lines
def format(self, record):
@ -345,8 +342,7 @@ class FluentFormatter(logging.Formatter):
message['context'] = {}
extra.pop('context', None)
# NOTE(vdrok): try to dump complex objects
primitive_types = six.string_types + six.integer_types + (
bool, type(None), float, list, dict)
primitive_types = (str, int, bool, type(None), float, list, dict)
for key, value in extra.items():
if not isinstance(value, primitive_types):
extra[key] = _json_dumps_with_fallback(value)
@ -399,20 +395,6 @@ class ContextFormatter(logging.Formatter):
def format(self, record):
"""Uses contextstring if request_id is set, otherwise default."""
if six.PY2:
should_use_unicode = True
args = (record.args.values() if isinstance(record.args, dict)
else record.args)
for arg in args or []:
try:
six.text_type(arg)
except UnicodeDecodeError:
should_use_unicode = False
break
if (not isinstance(record.msg, six.text_type)
and should_use_unicode):
record.msg = _ensure_unicode(record.msg)
# store project info
record.project = self.project
record.version = self.version
@ -523,16 +505,16 @@ class ContextFormatter(logging.Formatter):
return logging.Formatter.formatException(self, exc_info)
except TypeError as type_error:
# Work around https://bugs.python.org/issue28603
msg = six.text_type(type_error)
msg = str(type_error)
return '<Unprintable exception due to %s>\n' % msg
stringbuffer = moves.StringIO()
stringbuffer = io.StringIO()
try:
traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
None, stringbuffer)
except TypeError as type_error:
# Work around https://bugs.python.org/issue28603
msg = six.text_type(type_error)
msg = str(type_error)
stringbuffer.write('<Unprintable exception due to %s>\n' % msg)
lines = stringbuffer.getvalue().split('\n')

View File

@ -16,7 +16,6 @@ import logging
import logging.config
import logging.handlers
import os
import six
try:
from systemd import journal
@ -26,7 +25,6 @@ try:
import syslog
except ImportError:
syslog = None
from oslo_utils import encodeutils
NullHandler = logging.NullHandler
@ -71,29 +69,6 @@ class OSSysLogHandler(logging.Handler):
def emit(self, record):
priority = SYSLOG_MAP.get(record.levelname, 7)
message = self.format(record)
# NOTE(gangila): In python2, the syslog function takes in 's' as
# the format argument, which means it either accepts python string
# (str = 'a') or unicode strings (str = u'a'), the PyArg_ParseTuple
# then if needed converts the unicode objects to C strings using
# the *default encoding*. This default encoding is 'ascii' in case
# of python2 while it has been changed to 'utf-8' in case of
# python3. What this leads to is when we supply a syslog message
# like:
# >>> syslog.syslog(syslog.LOG_DEBUG, u"François Deppierraz")
# In case of python2 the above fails with TypeError: [priority,]
# message string. Because python2 doesn't explicitly encode as
# 'utf-8' and use the system default encoding ascii, which raises
# a UnicodeEncodeError (UnicodeEncodeError: 'ascii' codec can't
# encode character u'\xe7' in position 4: ordinal not in
# range(128)), and hence the error message that's set in the code
# (TypeError: [priority,] message string) gets shown to the user.
# However, this in the case of Python3, where the system default
# encoding is 'utf-8' works without any issues. Therefore, we need
# to safe_encode in case of python2 and not in case of Python3.
if six.PY2:
message = encodeutils.safe_encode(self.format(record))
syslog.syslog(priority, message)

View File

@ -27,6 +27,7 @@ It also allows setting of formatting information through conf.
"""
import configparser
import logging
import logging.config
import logging.handlers
@ -41,8 +42,6 @@ except ImportError:
from oslo_config import cfg
from oslo_utils import importutils
from oslo_utils import units
import six
from six import moves
from oslo_log._i18n import _
from oslo_log import _options
@ -232,8 +231,8 @@ def _load_log_config(log_config_append):
logging.config.fileConfig(log_config_append,
disable_existing_loggers=False)
_load_log_config.old_time = new_time
except (moves.configparser.Error, KeyError, os.error) as exc:
raise LogConfigError(log_config_append, six.text_type(exc))
except (configparser.Error, KeyError, os.error) as exc:
raise LogConfigError(log_config_append, str(exc))
def _mutate_hook(conf, fresh):

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import io
from oslo_log.cmds import convert_json
from oslo_serialization import jsonutils
@ -45,7 +45,7 @@ class ConvertJsonTestCase(test_base.BaseTestCase):
super(ConvertJsonTestCase, self).setUp()
def _reformat(self, text):
fh = six.StringIO(text)
fh = io.StringIO(text)
return list(convert_json.reformat_json(fh, lambda x: [x]))
def test_reformat_json_single(self):

View File

@ -19,6 +19,7 @@
from contextlib import contextmanager
import copy
import datetime
import io
import logging
import os
import platform
@ -40,7 +41,6 @@ from oslo_context import fixture as fixture_context
from oslo_i18n import fixture as fixture_trans
from oslo_serialization import jsonutils
from oslotest import base as test_base
import six
import testtools
from oslo_log import _options
@ -251,7 +251,7 @@ class LogTestBase(BaseTestCase):
:param formatter: The formatter class to set on the handler. Must be
the class itself, not an instance.
"""
self.stream = six.StringIO()
self.stream = io.StringIO()
if handler is None:
handler = logging.StreamHandler
self.handler = handler(self.stream)
@ -364,17 +364,12 @@ class OSSysLogHandlerTestCase(BaseTestCase):
def test_syslog(self):
msg_unicode = u"Benoît Knecht & François Deppierraz login failure"
msg_utf8 = msg_unicode.encode('utf-8')
handler = handlers.OSSysLogHandler()
syslog.syslog = mock.Mock()
handler.emit(
logging.LogRecord("name", logging.INFO, "path", 123,
msg_unicode, None, None))
if six.PY2:
syslog.syslog.assert_called_once_with(syslog.LOG_INFO, msg_utf8)
else:
syslog.syslog.assert_called_once_with(syslog.LOG_INFO, msg_unicode)
syslog.syslog.assert_called_once_with(syslog.LOG_INFO, msg_unicode)
class OSJournalHandlerTestCase(BaseTestCase):
@ -412,13 +407,13 @@ class OSJournalHandlerTestCase(BaseTestCase):
self.journal.send.call_args)
args, kwargs = self.journal.send.call_args
self.assertEqual(len(args), 1)
self.assertIsInstance(args[0], six.string_types)
self.assertIsInstance(args[0], str)
self.assertIsInstance(kwargs['CODE_LINE'], int)
self.assertIsInstance(kwargs['PRIORITY'], int)
del kwargs['CODE_LINE'], kwargs['PRIORITY']
for key, arg in kwargs.items():
self.assertIsInstance(key, six.string_types)
self.assertIsInstance(arg, six.string_types + (six.binary_type,))
self.assertIsInstance(key, str)
self.assertIsInstance(arg, (bytes, str))
def test_emit_exception(self):
l = log.getLogger('nova-exception.foo')
@ -443,13 +438,13 @@ class OSJournalHandlerTestCase(BaseTestCase):
self.journal.send.call_args)
args, kwargs = self.journal.send.call_args
self.assertEqual(len(args), 1)
self.assertIsInstance(args[0], six.string_types)
self.assertIsInstance(args[0], str)
self.assertIsInstance(kwargs['CODE_LINE'], int)
self.assertIsInstance(kwargs['PRIORITY'], int)
del kwargs['CODE_LINE'], kwargs['PRIORITY']
for key, arg in kwargs.items():
self.assertIsInstance(key, six.string_types)
self.assertIsInstance(arg, six.string_types + (six.binary_type,))
self.assertIsInstance(key, str)
self.assertIsInstance(arg, (bytes, str))
class LogLevelTestCase(BaseTestCase):
@ -610,15 +605,14 @@ class JSONFormatterTestCase(LogTestBase):
def test_can_process_strings(self):
expected = b'\\u2622'
if six.PY3:
# see ContextFormatterTestCase.test_can_process_strings
expected = '\\\\xe2\\\\x98\\\\xa2'
# see ContextFormatterTestCase.test_can_process_strings
expected = '\\\\xe2\\\\x98\\\\xa2'
self.log.info(b'%s', u'\u2622'.encode('utf8'))
self.assertIn(expected, self.stream.getvalue())
def test_exception(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
ctxt.request_id = str('99')
try:
raise RuntimeError('test_exception')
except RuntimeError:
@ -630,7 +624,7 @@ class JSONFormatterTestCase(LogTestBase):
def test_no_exception(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
ctxt.request_id = str('99')
self.log.info('testing', context=ctxt)
data = jsonutils.loads(self.stream.getvalue())
self.assertIn('error_summary', data)
@ -638,7 +632,7 @@ class JSONFormatterTestCase(LogTestBase):
def test_exception_without_exc_info_passed(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
ctxt.request_id = str('99')
try:
raise RuntimeError('test_exception\ntraceback\nfrom\nremote error')
except RuntimeError:
@ -649,7 +643,7 @@ class JSONFormatterTestCase(LogTestBase):
def test_exception_with_exc_info_passed(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
ctxt.request_id = str('99')
try:
raise RuntimeError('test_exception\ntraceback\nfrom\nremote error')
except RuntimeError:
@ -836,14 +830,14 @@ class ContextFormatterTestCase(LogTestBase):
def test_message_logging_3rd_party_log_records(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
ctxt.request_id = str('99')
sa_log = logging.getLogger('sqlalchemy.engine')
sa_log.setLevel(logging.INFO)
message = self.trans_fixture.lazy('test ' + six.unichr(128))
message = self.trans_fixture.lazy('test ' + chr(128))
sa_log.info(message)
expected = ('HAS CONTEXT [%s]: %s\n' % (ctxt.request_id,
six.text_type(message)))
str(message)))
self.assertEqual(expected, self.stream.getvalue())
def test_debugging_log(self):
@ -858,11 +852,11 @@ class ContextFormatterTestCase(LogTestBase):
# the Message object, with a wrong encoding. This test case
# tests that problem does not occur.
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = self.trans_fixture.lazy('test ' + six.unichr(128))
ctxt.request_id = str('99')
message = self.trans_fixture.lazy('test ' + chr(128))
self.log.info(message, context=ctxt)
expected = "HAS CONTEXT [%s]: %s\n" % (ctxt.request_id,
six.text_type(message))
str(message))
self.assertEqual(expected, self.stream.getvalue())
def test_exception_logging(self):
@ -870,8 +864,8 @@ class ContextFormatterTestCase(LogTestBase):
# does not appear in the format string, ensure that it is
# appended to the end of the log lines.
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = self.trans_fixture.lazy('test ' + six.unichr(128))
ctxt.request_id = str('99')
message = self.trans_fixture.lazy('test ' + chr(128))
try:
raise RuntimeError('test_exception_logging')
except RuntimeError:
@ -883,8 +877,8 @@ class ContextFormatterTestCase(LogTestBase):
# NOTE(dhellmann): Several of the built-in exception types
# should not be automatically added to the log output.
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = self.trans_fixture.lazy('test ' + six.unichr(128))
ctxt.request_id = str('99')
message = self.trans_fixture.lazy('test ' + chr(128))
ignored_exceptions = [
ValueError, TypeError, KeyError, AttributeError, ImportError
]
@ -902,8 +896,8 @@ class ContextFormatterTestCase(LogTestBase):
# that position in the output.
self.config(logging_context_format_string="A %(error_summary)s B")
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = self.trans_fixture.lazy('test ' + six.unichr(128))
ctxt.request_id = str('99')
message = self.trans_fixture.lazy('test ' + chr(128))
try:
raise RuntimeError('test_exception_logging')
except RuntimeError:
@ -917,32 +911,32 @@ class ContextFormatterTestCase(LogTestBase):
# inserted.
self.config(logging_context_format_string="%(error_summary)s")
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = self.trans_fixture.lazy('test ' + six.unichr(128))
ctxt.request_id = str('99')
message = self.trans_fixture.lazy('test ' + chr(128))
self.log.info(message, context=ctxt)
expected = '-\n'
self.assertTrue(self.stream.getvalue().startswith(expected))
def test_unicode_conversion_in_adapter(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
ctxt.request_id = str('99')
message = "Exception is (%s)"
ex = Exception(self.trans_fixture.lazy('test' + six.unichr(128)))
ex = Exception(self.trans_fixture.lazy('test' + chr(128)))
self.log.debug(message, ex, context=ctxt)
message = six.text_type(message) % ex
message = str(message) % ex
expected = "HAS CONTEXT [%s]: %s --DBG\n" % (ctxt.request_id,
message)
self.assertEqual(expected, self.stream.getvalue())
def test_unicode_conversion_in_formatter(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
ctxt.request_id = str('99')
no_adapt_log = logging.getLogger('no_adapt')
no_adapt_log.setLevel(logging.INFO)
message = "Exception is (%s)"
ex = Exception(self.trans_fixture.lazy('test' + six.unichr(128)))
ex = Exception(self.trans_fixture.lazy('test' + chr(128)))
no_adapt_log.info(message, ex)
message = six.text_type(message) % ex
message = str(message) % ex
expected = "HAS CONTEXT [%s]: %s\n" % (ctxt.request_id,
message)
self.assertEqual(expected, self.stream.getvalue())
@ -959,7 +953,7 @@ class ContextFormatterTestCase(LogTestBase):
expected = ("HAS CONTEXT [%s %s %s %s %s %s]: %s\n" %
(ctxt.request_id, ctxt.user, ctxt.tenant, ctxt.domain,
ctxt.user_domain, ctxt.project_domain,
six.text_type(message)))
str(message)))
self.assertEqual(expected, self.stream.getvalue())
def test_user_identity_logging_set_format(self):
@ -975,7 +969,7 @@ class ContextFormatterTestCase(LogTestBase):
self.log.info(message, context=ctxt)
expected = ("HAS CONTEXT [%s %s %s]: %s\n" %
(ctxt.request_id, ctxt.user, ctxt.tenant,
six.text_type(message)))
str(message)))
self.assertEqual(expected, self.stream.getvalue())
@mock.patch("datetime.datetime",
@ -1009,11 +1003,10 @@ class ContextFormatterTestCase(LogTestBase):
def test_can_process_strings(self):
expected = b'\xe2\x98\xa2'
if six.PY3:
# in PY3 logging format string should be unicode string
# or it will fail and inserting byte string in unicode string
# causes such formatting
expected = '\\xe2\\x98\\xa2'
# logging format string should be unicode string
# or it will fail and inserting byte string in unicode string
# causes such formatting
expected = '\\xe2\\x98\\xa2'
self.log.info(b'%s', u'\u2622'.encode('utf8'))
self.assertIn(expected, self.stream.getvalue())
@ -1101,7 +1094,7 @@ class FancyRecordTestCase(LogTestBase):
# and goes to stderr. Suggests on a better way to do this are
# welcomed.
error = sys.stderr
sys.stderr = six.StringIO()
sys.stderr = io.StringIO()
self.colorlog.info("foo")
self.assertNotEqual(-1,
@ -1608,7 +1601,7 @@ keys=
root = logging.getLogger()
self.assertEqual(1, len(root.handlers))
handler = root.handlers[0]
handler.stream = six.StringIO()
handler.stream = io.StringIO()
return handler.stream
def test_remove_handler(self):
@ -1634,7 +1627,7 @@ keys=
'loggers': {'a.a': fake_logger}}
conf2 = {'root': {'handlers': 'fake'},
'handlers': {'fake': fake_handler}}
stream = six.StringIO()
stream = io.StringIO()
with self.mutate_conf(conf1, conf2) as (loginis, confs):
stream = self.set_root_stream()
log = logging.getLogger("a.a")
@ -1653,7 +1646,7 @@ class LogConfigOptsTestCase(BaseTestCase):
super(LogConfigOptsTestCase, self).setUp()
def test_print_help(self):
f = six.StringIO()
f = io.StringIO()
self.CONF([])
self.CONF.print_help(file=f)
for option in ['debug', 'log-config', 'watch-log-file']:
@ -1920,20 +1913,20 @@ class UnicodeConversionTestCase(BaseTestCase):
enc_msg = msg.encode('utf-8')
result = formatters._ensure_unicode(enc_msg)
self.assertEqual(msg, result)
self.assertIsInstance(result, six.text_type)
self.assertIsInstance(result, str)
def test_unicode_to_unicode(self):
msg = self._MSG
result = formatters._ensure_unicode(msg)
self.assertEqual(msg, result)
self.assertIsInstance(result, six.text_type)
self.assertIsInstance(result, str)
def test_exception_to_unicode(self):
msg = self._MSG
exc = Exception(msg)
result = formatters._ensure_unicode(exc)
self.assertEqual(msg, result)
self.assertIsInstance(result, six.text_type)
self.assertIsInstance(result, str)
class LoggerNameTestCase(LoggerTestCase):

View File

@ -12,11 +12,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import logging
import mock
from oslotest import base as test_base
import six
from oslo_log import rate_limit
@ -41,7 +41,7 @@ class LogRateLimitTestCase(test_base.BaseTestCase):
logger.removeHandler(handler)
# install our handler writing logs into a StringIO
stream = six.StringIO()
stream = io.StringIO()
handler = logging.StreamHandler(stream)
logger.addHandler(handler)

View File

@ -13,11 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import unittest
import mock
from oslotest import base as test_base
import six
from testtools import matchers
from oslo_log import versionutils
@ -248,27 +245,6 @@ class DeprecatedTestCase(test_base.BaseTestCase):
as_of='Juno',
remove_in='Kilo')
@unittest.skipIf(
six.PY3,
'Deprecated exception detection does not work for Python 3')
@mock.patch('oslo_log.versionutils.report_deprecated_feature')
def test_deprecated_exception(self, mock_log):
@versionutils.deprecated(as_of=versionutils.deprecated.ICEHOUSE,
remove_in=+1)
class OldException(Exception):
pass
class NewException(OldException):
pass
try:
raise NewException()
except OldException:
pass
self.assert_deprecated(mock_log, what='OldException()',
as_of='Icehouse', remove_in='Juno')
@mock.patch('oslo_log.versionutils.report_deprecated_feature')
def test_deprecated_exception_old(self, mock_log):
@versionutils.deprecated(as_of=versionutils.deprecated.ICEHOUSE,

View File

@ -22,7 +22,6 @@ import inspect
import logging
from oslo_config import cfg
import six
from oslo_log._i18n import _
@ -180,7 +179,7 @@ class deprecated(object):
if inspect.isfunction(func_or_cls):
@six.wraps(func_or_cls)
@functools.wraps(func_or_cls)
def wrapped(*args, **kwargs):
report_deprecated()
return func_or_cls(*args, **kwargs)
@ -188,7 +187,7 @@ class deprecated(object):
elif inspect.isclass(func_or_cls):
orig_init = func_or_cls.__init__
@six.wraps(orig_init, assigned=('__name__', '__doc__'))
@functools.wraps(orig_init, assigned=('__name__', '__doc__'))
def new_init(self, *args, **kwargs):
if self.__class__ in _DEPRECATED_EXCEPTIONS:
report_deprecated()
@ -213,7 +212,7 @@ class deprecated(object):
report_deprecated()
return super(ExceptionMeta,
self).__subclasscheck__(subclass)
func_or_cls = six.add_metaclass(ExceptionMeta)(func_or_cls)
func_or_cls.__meta__ = ExceptionMeta
_DEPRECATED_EXCEPTIONS.add(func_or_cls)
return func_or_cls

View File

@ -3,7 +3,6 @@
# process, which may cause wedges in the gate later.
pbr>=3.1.1 # Apache-2.0
six>=1.11.0 # MIT
oslo.config>=5.2.0 # Apache-2.0
oslo.context>=2.20.0 # Apache-2.0
oslo.i18n>=3.20.0 # Apache-2.0