Drop use of six

Another one bites the dust.

Change-Id: I1fadcad8219322b569eeecd81e454a44641e8b1e
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2020-02-27 15:48:35 +00:00
parent 9fee0c4f48
commit ad356ea9c2
8 changed files with 44 additions and 65 deletions

View File

@ -33,7 +33,6 @@ pytz==2013.6
PyYAML==3.12
requests==2.14.2
requestsexceptions==1.2.0
six==1.10.0
smmap==0.9.0
stestr==2.0.0
stevedore==1.20.0

View File

@ -24,8 +24,6 @@ from __future__ import absolute_import
import base64
import binascii
import six
def encode_as_bytes(s, encoding='utf-8'):
"""Encode a string using Base64.
@ -38,7 +36,7 @@ def encode_as_bytes(s, encoding='utf-8'):
Use encode_as_text() to get the Base64 encoded string as text.
"""
if isinstance(s, six.text_type):
if isinstance(s, str):
s = s.encode(encoding)
return base64.b64encode(s)

View File

@ -37,12 +37,11 @@ import itertools
import json
import uuid
import warnings
from xmlrpc import client as xmlrpclib
from oslo_utils import encodeutils
from oslo_utils import importutils
from oslo_utils import timeutils
import six
import six.moves.xmlrpc_client as xmlrpclib
ipaddress = importutils.try_import("ipaddress")
netaddr = importutils.try_import("netaddr")
@ -53,8 +52,7 @@ _nasty_type_tests = [inspect.ismodule, inspect.isclass, inspect.ismethod,
inspect.iscode, inspect.isbuiltin, inspect.isroutine,
inspect.isabstract]
_simple_types = ((six.text_type,) + six.integer_types +
(type(None), bool, float))
_simple_types = (str, int, type(None), bool, float)
def to_primitive(value, convert_instances=False, convert_datetime=True,
@ -86,7 +84,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
"""
orig_fallback = fallback
if fallback is None:
fallback = six.text_type
fallback = str
# handle obvious types first - order of basic types determined by running
# full tests on nova project, resulting in the following counts:
@ -104,10 +102,8 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
if isinstance(value, _simple_types):
return value
if isinstance(value, six.binary_type):
if six.PY3:
value = value.decode(encoding=encoding)
return value
if isinstance(value, bytes):
return value.decode(encoding=encoding)
# It's not clear why xmlrpclib created their own DateTime type, but
# for our purposes, make it a datetime type which is explicitly
@ -122,15 +118,15 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
return value
if isinstance(value, uuid.UUID):
return six.text_type(value)
return str(value)
if netaddr and isinstance(value, (netaddr.IPAddress, netaddr.IPNetwork)):
return six.text_type(value)
return str(value)
if ipaddress and isinstance(value,
(ipaddress.IPv4Address,
ipaddress.IPv6Address)):
return six.text_type(value)
return str(value)
# For exceptions, return the 'repr' of the exception object
if isinstance(value, Exception):
@ -219,7 +215,7 @@ def dump_as_bytes(obj, default=to_primitive, encoding='utf-8', **kwargs):
.. versionadded:: 1.10
"""
serialized = dumps(obj, default=default, **kwargs)
if isinstance(serialized, six.text_type):
if isinstance(serialized, str):
# On Python 3, json.dumps() returns Unicode
serialized = serialized.encode(encoding)
return serialized

View File

@ -32,12 +32,11 @@ import datetime
import functools
import itertools
import uuid
from xmlrpc import client as xmlrpclib
import msgpack
from oslo_utils import importutils
from pytz import timezone
import six
import six.moves.xmlrpc_client as xmlrpclib
netaddr = importutils.try_import("netaddr")
@ -126,7 +125,7 @@ class HandlerRegistry(object):
def __iter__(self):
"""Iterates over **all** registered handlers."""
for handlers in six.itervalues(self._handlers):
for handlers in self._handlers.values():
for h in handlers:
yield h
@ -196,7 +195,7 @@ class HandlerRegistry(object):
def match(self, obj):
"""Match the registries handlers to the given object (or none)."""
for possible_handlers in six.itervalues(self._handlers):
for possible_handlers in self._handlers.values():
for h in possible_handlers:
if isinstance(obj, h.handles):
return h
@ -209,11 +208,11 @@ class UUIDHandler(object):
@staticmethod
def serialize(obj):
return six.text_type(obj.hex).encode('ascii')
return str(obj.hex).encode('ascii')
@staticmethod
def deserialize(data):
return uuid.UUID(hex=six.text_type(data, encoding='ascii'))
return uuid.UUID(hex=str(data, encoding='ascii'))
class DateTimeHandler(object):
@ -238,15 +237,13 @@ class DateTimeHandler(object):
}
if dt.tzinfo:
tz = dt.tzinfo.tzname(None)
if six.PY2:
tz = tz.decode("ascii")
dct[u'tz'] = tz
return dumps(dct, registry=self._registry)
def deserialize(self, blob):
dct = loads(blob, registry=self._registry)
if six.PY3 and b"day" in dct:
if b"day" in dct:
# NOTE(sileht): oslo.serialization <= 2.4.1 was
# storing thing as unicode for py3 while is was
# bytes for py2
@ -280,7 +277,7 @@ class CountHandler(object):
def serialize(obj):
# FIXME(harlowja): figure out a better way to avoid hacking into
# the string representation of count to get at the right numbers...
obj = six.text_type(obj)
obj = str(obj)
start = obj.find("(") + 1
end = obj.rfind(")")
pieces = obj[start:end].split(",")
@ -376,7 +373,7 @@ class DateHandler(object):
def deserialize(self, blob):
dct = loads(blob, registry=self._registry)
if six.PY3 and b"day" in dct:
if b"day" in dct:
# NOTE(sileht): see DateTimeHandler.deserialize()
dct = dict((k.decode("ascii"), v) for k, v in dct.items())

View File

@ -18,11 +18,9 @@ Unified and simplified API for oslo.serialization's serializers.
import abc
import six
@six.add_metaclass(abc.ABCMeta)
class BaseSerializer(object):
class BaseSerializer(object, metaclass=abc.ABCMeta):
"""Generic (de-)serialization definition abstract base class."""
@abc.abstractmethod

View File

@ -16,16 +16,16 @@
import collections
import datetime
import functools
import io
import ipaddress
import itertools
import json
from xmlrpc import client as xmlrpclib
import mock
import netaddr
from oslo_i18n import fixture
from oslotest import base as test_base
import six
import six.moves.xmlrpc_client as xmlrpclib
from oslo_serialization import jsonutils
@ -69,7 +69,7 @@ class JSONUtilsTestMixin(object):
expected = '{"a": "b"}'
json_dict = {'a': 'b'}
fp = six.StringIO()
fp = io.StringIO()
jsonutils.dump(json_dict, fp)
self.assertEqual(expected, fp.getvalue())
@ -78,7 +78,7 @@ class JSONUtilsTestMixin(object):
expected = '[1, 2]'
json_dict = collections.namedtuple("foo", "bar baz")(1, 2)
fp = six.StringIO()
fp = io.StringIO()
jsonutils.dump(json_dict, fp)
self.assertEqual(expected, fp.getvalue())
@ -87,15 +87,15 @@ class JSONUtilsTestMixin(object):
self.assertEqual({'a': 'b'}, jsonutils.loads('{"a": "b"}'))
def test_loads_unicode(self):
self.assertIsInstance(jsonutils.loads(b'"foo"'), six.text_type)
self.assertIsInstance(jsonutils.loads(u'"foo"'), six.text_type)
self.assertIsInstance(jsonutils.loads(b'"foo"'), str)
self.assertIsInstance(jsonutils.loads(u'"foo"'), str)
# 'test' in Ukrainian
i18n_str_unicode = u'"\u0442\u0435\u0441\u0442"'
self.assertIsInstance(jsonutils.loads(i18n_str_unicode), six.text_type)
self.assertIsInstance(jsonutils.loads(i18n_str_unicode), str)
i18n_str = i18n_str_unicode.encode('utf-8')
self.assertIsInstance(jsonutils.loads(i18n_str), six.text_type)
self.assertIsInstance(jsonutils.loads(i18n_str), str)
def test_loads_with_kwargs(self):
jsontext = u'{"foo": 3}'
@ -108,12 +108,12 @@ class JSONUtilsTestMixin(object):
expected = {u'a': u'\u0442\u044d\u0441\u0442'}
for encoding in ('utf-8', 'cp1251'):
fp = six.BytesIO(jsontext.encode(encoding))
fp = io.BytesIO(jsontext.encode(encoding))
result = jsonutils.load(fp, encoding=encoding)
self.assertEqual(expected, result)
for key, val in result.items():
self.assertIsInstance(key, six.text_type)
self.assertIsInstance(val, six.text_type)
self.assertIsInstance(key, str)
self.assertIsInstance(val, str)
def test_dumps_exception_value(self):
self.assertIn(jsonutils.dumps({"a": ValueError("hello")}),
@ -277,10 +277,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
def test_typeerror(self):
x = bytearray # Class, not instance
if six.PY3:
self.assertEqual(u"<class 'bytearray'>", jsonutils.to_primitive(x))
else:
self.assertEqual(u"<type 'bytearray'>", jsonutils.to_primitive(x))
self.assertEqual(u"<class 'bytearray'>", jsonutils.to_primitive(x))
def test_nasties(self):
def foo():
@ -290,12 +287,9 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
self.assertEqual(3, len(ret))
self.assertTrue(ret[0].startswith(u"<module 'datetime' from ") or
ret[0].startswith(u"<module 'datetime' (built-in)"))
if six.PY3:
self.assertTrue(ret[1].startswith(
'<function ToPrimitiveTestCase.test_nasties.<locals>.foo at 0x'
))
else:
self.assertTrue(ret[1].startswith('<function foo at 0x'))
self.assertTrue(ret[1].startswith(
'<function ToPrimitiveTestCase.test_nasties.<locals>.foo at 0x'
))
self.assertEqual('<built-in function dir>', ret[2])
def test_depth(self):
@ -379,7 +373,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
obj = itertools.count(1)
ret = jsonutils.to_primitive(obj)
self.assertEqual(six.text_type(obj), ret)
self.assertEqual(str(obj), ret)
ret = jsonutils.to_primitive(obj, fallback=lambda _: 'itertools_count')
self.assertEqual('itertools_count', ret)
@ -387,7 +381,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
def test_fallback_nasty(self):
obj = int
ret = jsonutils.to_primitive(obj)
self.assertEqual(six.text_type(obj), ret)
self.assertEqual(str(obj), ret)
def formatter(typeobj):
return 'type:%s' % typeobj.__name__
@ -402,7 +396,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
obj = NotIterable()
ret = jsonutils.to_primitive(obj)
self.assertEqual(six.text_type(obj), ret)
self.assertEqual(str(obj), ret)
ret = jsonutils.to_primitive(obj, fallback=lambda _: 'fallback')
self.assertEqual('fallback', ret)

View File

@ -14,12 +14,11 @@
import datetime
import itertools
from xmlrpc import client as xmlrpclib
import netaddr
from oslotest import base as test_base
from pytz import timezone
import six
import six.moves.xmlrpc_client as xmlrpclib
from oslo_serialization import msgpackutils
from oslo_utils import uuidutils
@ -44,8 +43,7 @@ class ColorHandler(object):
@staticmethod
def serialize(obj):
blob = '%s, %s, %s' % (obj.r, obj.g, obj.b)
if six.PY3:
blob = blob.encode("ascii")
blob = blob.encode('ascii')
return blob
@staticmethod
@ -103,19 +101,19 @@ class MsgPackUtilsTest(test_base.BaseTestCase):
def test_itercount(self):
it = itertools.count(1)
six.next(it)
six.next(it)
next(it)
next(it)
it2 = _dumps_loads(it)
self.assertEqual(six.next(it), six.next(it2))
self.assertEqual(next(it), next(it2))
it = itertools.count(0)
it2 = _dumps_loads(it)
self.assertEqual(six.next(it), six.next(it2))
self.assertEqual(next(it), next(it2))
def test_itercount_step(self):
it = itertools.count(1, 3)
it2 = _dumps_loads(it)
self.assertEqual(six.next(it), six.next(it2))
self.assertEqual(next(it), next(it2))
def test_set(self):
self.assertEqual(set([1, 2]), _dumps_loads(set([1, 2])))

View File

@ -8,7 +8,6 @@
# that is a likely indicator that the feature belongs somewhere else.
pbr!=2.1.0,>=2.0.0 # Apache-2.0
six>=1.10.0 # MIT
msgpack>=0.5.2 # Apache-2.0
oslo.utils>=3.33.0 # Apache-2.0
pytz>=2013.6 # MIT