From 0002625e2b06ec6c8c76d992a1a0cf1cb013e2de Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 14 Jul 2021 10:16:47 +0100 Subject: [PATCH] utils: Remove unicode-related helpers The 'unicode_key_value_to_string' and '_encode' helpers were used to handle the difference between unicode and byte strings in Python 2. In a Python 3-only world, they are unnecessary. Remove them. Some references to text encoding in the HACKING file are removed. In theory, we should no longer have to worry about byte strings. Change-Id: I0694b25dc4d72e817cb08ce6cafb39bb8226293d Signed-off-by: Stephen Finucane --- HACKING | 40 ------------------------ manilaclient/base.py | 1 - manilaclient/tests/unit/osc/osc_fakes.py | 14 --------- manilaclient/tests/unit/test_utils.py | 24 -------------- manilaclient/utils.py | 16 ---------- 5 files changed, 95 deletions(-) delete mode 100644 manilaclient/tests/unit/test_utils.py diff --git a/HACKING b/HACKING index 0b8411e65..5711f929a 100644 --- a/HACKING +++ b/HACKING @@ -73,43 +73,3 @@ Docstrings :returns: description of the return value """ - -Text encoding -------------- -- All text within python code should be of type 'unicode'. - - WRONG: - - >>> s = 'foo' - >>> s - 'foo' - >>> type(s) - - - RIGHT: - - >>> u = u'foo' - >>> u - u'foo' - >>> type(u) - - -- Transitions between internal unicode and external strings should always - be immediately and explicitly encoded or decoded. - -- All external text that is not explicitly encoded (database storage, - commandline arguments, etc.) should be presumed to be encoded as utf-8. - - WRONG: - - mystring = infile.readline() - myreturnstring = do_some_magic_with(mystring) - outfile.write(myreturnstring) - - RIGHT: - - mystring = infile.readline() - mytext = s.decode('utf-8') - returntext = do_some_magic_with(mytext) - returnstring = returntext.encode('utf-8') - outfile.write(returnstring) diff --git a/manilaclient/base.py b/manilaclient/base.py index 491096137..fd24819a1 100644 --- a/manilaclient/base.py +++ b/manilaclient/base.py @@ -180,7 +180,6 @@ class Manager(utils.HookableMixin): def _build_query_string(self, search_opts): search_opts = search_opts or {} - search_opts = utils.unicode_key_value_to_string(search_opts) params = sorted( [(k, v) for (k, v) in search_opts.items() if v]) query_string = "?%s" % utils.safe_urlencode(params) if params else '' diff --git a/manilaclient/tests/unit/osc/osc_fakes.py b/manilaclient/tests/unit/osc/osc_fakes.py index e37089948..702834be9 100644 --- a/manilaclient/tests/unit/osc/osc_fakes.py +++ b/manilaclient/tests/unit/osc/osc_fakes.py @@ -48,20 +48,6 @@ TEST_RESPONSE_DICT_V3.set_project_scope() TEST_VERSIONS = fixture.DiscoveryList(href=AUTH_URL) -def to_unicode_dict(catalog_dict): - """Converts dict to unicode dict""" - - if isinstance(catalog_dict, dict): - return {to_unicode_dict(key): to_unicode_dict(value) - for key, value in catalog_dict.items()} - elif isinstance(catalog_dict, list): - return [to_unicode_dict(element) for element in catalog_dict] - elif isinstance(catalog_dict, str): - return catalog_dict + u"" - else: - return catalog_dict - - class FakeStdout(object): def __init__(self): diff --git a/manilaclient/tests/unit/test_utils.py b/manilaclient/tests/unit/test_utils.py deleted file mode 100644 index add29874a..000000000 --- a/manilaclient/tests/unit/test_utils.py +++ /dev/null @@ -1,24 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import testtools - -from manilaclient import utils - - -class TestCommonUtils(testtools.TestCase): - - def test_unicode_key_value_to_string(self): - src = {u'key': u'\u70fd\u7231\u5a77'} - # u'xxxx' in PY3 is str, we will not get extra 'u' from cli - # output in PY3 - self.assertEqual(src, utils.unicode_key_value_to_string(src)) diff --git a/manilaclient/utils.py b/manilaclient/utils.py index 61c5502d9..8dcc477bc 100644 --- a/manilaclient/utils.py +++ b/manilaclient/utils.py @@ -47,22 +47,6 @@ def get_function_name(func): return "%s.%s" % (func.__module__, func.__qualname__) -def _encode(src): - """remove extra 'u' in PY2.""" - return src - - -def unicode_key_value_to_string(src): - """Recursively converts dictionary keys to strings.""" - if isinstance(src, dict): - return dict((_encode(k), - _encode(unicode_key_value_to_string(v))) - for k, v in src.items()) - if isinstance(src, list): - return [unicode_key_value_to_string(l) for l in src] - return _encode(src) - - def safe_urlencode(params_dict): """Workaround incompatible change to urllib.parse