Python 3: use six.string_types instead of basestring

In Python 3, there is no "basestring". In Python 3, "six.string_types" is
"basestring", and "str" in Python 3.

Change-Id: Ic22e932cbf3c4b75cd424f4b41428da869f197cf
Blueprint: neutron-python3
This commit is contained in:
Cyril Roelandt 2015-05-20 15:09:13 +02:00
parent d825ef947f
commit 6c1cb05302
10 changed files with 31 additions and 12 deletions

View File

@ -15,6 +15,7 @@ Neutron Specific Commandments
- [N323] Enforce namespace-less imports for oslo libraries
- [N324] Prevent use of deprecated contextlib.nested.
- [N325] Python 3: Do not use xrange.
- [N326] Python 3: do not use basestring.
Creating Unit Tests
-------------------

View File

@ -19,6 +19,7 @@ import uuid
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import excutils
import six
from neutron.agent.common import utils
from neutron.agent.ovsdb import api as ovsdb
@ -255,7 +256,7 @@ def _set_colval_args(*col_values):
args += ["%s:%s%s%s" % (
col, k, op, _py_to_val(v)) for k, v in val.items()]
elif (isinstance(val, collections.Sequence)
and not isinstance(val, basestring)):
and not isinstance(val, six.string_types)):
args.append("%s%s%s" % (col, op, ",".join(map(_py_to_val, val))))
else:
args.append("%s%s%s" % (col, op, _py_to_val(val)))

View File

@ -17,6 +17,7 @@ import re
import netaddr
from oslo_log import log as logging
import six
from neutron.common import constants
from neutron.common import exceptions as n_exc
@ -105,7 +106,7 @@ def _validate_string_or_none(data, max_len=None):
def _validate_string(data, max_len=None):
if not isinstance(data, basestring):
if not isinstance(data, six.string_types):
msg = _("'%s' is not a valid string") % data
LOG.debug(msg)
return msg
@ -481,7 +482,7 @@ def _validate_non_negative(data, valid_values=None):
def convert_to_boolean(data):
if isinstance(data, basestring):
if isinstance(data, six.string_types):
val = data.lower()
if val == "true" or val == "1":
return True

View File

@ -15,6 +15,7 @@
import weakref
import six
from sqlalchemy import sql
from neutron.common import exceptions as n_exc
@ -91,13 +92,13 @@ class CommonDbMixin(object):
for _name, hooks in self._model_query_hooks.get(model,
{}).iteritems():
query_hook = hooks.get('query')
if isinstance(query_hook, basestring):
if isinstance(query_hook, six.string_types):
query_hook = getattr(self, query_hook, None)
if query_hook:
query = query_hook(context, model, query)
filter_hook = hooks.get('filter')
if isinstance(filter_hook, basestring):
if isinstance(filter_hook, six.string_types):
filter_hook = getattr(self, filter_hook, None)
if filter_hook:
query_filter = filter_hook(context, model, query_filter)
@ -141,7 +142,7 @@ class CommonDbMixin(object):
for _name, hooks in self._model_query_hooks.get(model,
{}).iteritems():
result_filter = hooks.get('result_filters', None)
if isinstance(result_filter, basestring):
if isinstance(result_filter, six.string_types):
result_filter = getattr(self, result_filter, None)
if result_filter:
@ -153,7 +154,7 @@ class CommonDbMixin(object):
for func in self._dict_extend_functions.get(
resource_type, []):
args = (response, db_object)
if isinstance(func, basestring):
if isinstance(func, six.string_types):
func = getattr(self, func, None)
else:
# must call unbound method - use self as 1st argument

View File

@ -16,6 +16,7 @@ import collections
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import excutils
import six
from neutron.api.v2 import attributes
from neutron.callbacks import events
@ -165,7 +166,7 @@ class L3_NAT_with_dvr_db_mixin(l3_db.L3_NAT_db_mixin,
def _get_device_owner(self, context, router=None):
"""Get device_owner for the specified router."""
router_is_uuid = isinstance(router, basestring)
router_is_uuid = isinstance(router, six.string_types)
if router_is_uuid:
router = self._get_router(context, router)
if is_distributed_router(router):

View File

@ -20,6 +20,7 @@ from alembic import autogenerate as autogen
from alembic import context
from alembic import op
import six
import sqlalchemy
from sqlalchemy import schema as sa_schema
import sqlalchemy.sql.expression as expr
@ -113,12 +114,12 @@ def parse_modify_command(command):
return
elif modified.endswith('default'):
modified = 'server_default'
if isinstance(new, basestring):
if isinstance(new, six.string_types):
new = text(new)
kwargs = {modified: new, 'schema': schema}
default = existing.get('existing_server_default')
if default and isinstance(default, sa_schema.DefaultClause):
if isinstance(default.arg, basestring):
if isinstance(default.arg, six.string_types):
existing['existing_server_default'] = default.arg
else:
existing['existing_server_default'] = default.arg.text

View File

@ -151,7 +151,7 @@ def convert_protocol(value):
def convert_ethertype_to_case_insensitive(value):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
for ethertype in sg_supported_ethertypes:
if ethertype.lower() == value.lower():
return ethertype

View File

@ -166,6 +166,13 @@ def check_python3_xrange(logical_line):
"large loops.")
def check_no_basestring(logical_line):
if re.search(r"\bbasestring\b", logical_line):
msg = ("N326: basestring is not Python3-compatible, use "
"six.string_types instead.")
yield(0, msg)
def factory(register):
register(validate_log_translations)
register(use_jsonutils)
@ -174,3 +181,4 @@ def factory(register):
register(check_oslo_namespace_imports)
register(check_no_contextlib_nested)
register(check_python3_xrange)
register(check_no_basestring)

View File

@ -19,6 +19,7 @@ import sys
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
import six
import webob
from neutron.common import exceptions
@ -219,7 +220,7 @@ class QuotaEngine(object):
_driver_class = QUOTA_CONF_DRIVER
LOG.info(_LI("ConfDriver is used as quota_driver because the "
"loaded plugin does not support 'quotas' table."))
if isinstance(_driver_class, basestring):
if isinstance(_driver_class, six.string_types):
_driver_class = importutils.import_object(_driver_class)
if isinstance(_driver_class, ConfDriver):
versionutils.report_deprecated_feature(

View File

@ -129,3 +129,7 @@ class HackingTestCase(base.BaseTestCase):
self.assertLineFails(f, 'c = xrange(1, 10, 2)')
self.assertLinePasses(f, 'd = range(1000)')
self.assertLinePasses(f, 'e = six.moves.range(1337)')
def test_no_basestring(self):
self.assertEqual(1,
len(list(checks.check_no_basestring("isinstance(x, basestring)"))))