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 PyYAML==3.12
requests==2.14.2 requests==2.14.2
requestsexceptions==1.2.0 requestsexceptions==1.2.0
six==1.10.0
smmap==0.9.0 smmap==0.9.0
stestr==2.0.0 stestr==2.0.0
stevedore==1.20.0 stevedore==1.20.0

View File

@ -24,8 +24,6 @@ from __future__ import absolute_import
import base64 import base64
import binascii import binascii
import six
def encode_as_bytes(s, encoding='utf-8'): def encode_as_bytes(s, encoding='utf-8'):
"""Encode a string using Base64. """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. 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) s = s.encode(encoding)
return base64.b64encode(s) return base64.b64encode(s)

View File

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

View File

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

View File

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

View File

@ -14,12 +14,11 @@
import datetime import datetime
import itertools import itertools
from xmlrpc import client as xmlrpclib
import netaddr import netaddr
from oslotest import base as test_base from oslotest import base as test_base
from pytz import timezone from pytz import timezone
import six
import six.moves.xmlrpc_client as xmlrpclib
from oslo_serialization import msgpackutils from oslo_serialization import msgpackutils
from oslo_utils import uuidutils from oslo_utils import uuidutils
@ -44,8 +43,7 @@ class ColorHandler(object):
@staticmethod @staticmethod
def serialize(obj): def serialize(obj):
blob = '%s, %s, %s' % (obj.r, obj.g, obj.b) blob = '%s, %s, %s' % (obj.r, obj.g, obj.b)
if six.PY3: blob = blob.encode('ascii')
blob = blob.encode("ascii")
return blob return blob
@staticmethod @staticmethod
@ -103,19 +101,19 @@ class MsgPackUtilsTest(test_base.BaseTestCase):
def test_itercount(self): def test_itercount(self):
it = itertools.count(1) it = itertools.count(1)
six.next(it) next(it)
six.next(it) next(it)
it2 = _dumps_loads(it) it2 = _dumps_loads(it)
self.assertEqual(six.next(it), six.next(it2)) self.assertEqual(next(it), next(it2))
it = itertools.count(0) it = itertools.count(0)
it2 = _dumps_loads(it) it2 = _dumps_loads(it)
self.assertEqual(six.next(it), six.next(it2)) self.assertEqual(next(it), next(it2))
def test_itercount_step(self): def test_itercount_step(self):
it = itertools.count(1, 3) it = itertools.count(1, 3)
it2 = _dumps_loads(it) it2 = _dumps_loads(it)
self.assertEqual(six.next(it), six.next(it2)) self.assertEqual(next(it), next(it2))
def test_set(self): def test_set(self):
self.assertEqual(set([1, 2]), _dumps_loads(set([1, 2]))) 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. # that is a likely indicator that the feature belongs somewhere else.
pbr!=2.1.0,>=2.0.0 # Apache-2.0 pbr!=2.1.0,>=2.0.0 # Apache-2.0
six>=1.10.0 # MIT
msgpack>=0.5.2 # Apache-2.0 msgpack>=0.5.2 # Apache-2.0
oslo.utils>=3.33.0 # Apache-2.0 oslo.utils>=3.33.0 # Apache-2.0
pytz>=2013.6 # MIT pytz>=2013.6 # MIT