Skip to content

Commit

Permalink
Remove six
Browse files Browse the repository at this point in the history
This mostly affects tests. Nothing too complicated

Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Change-Id: Iabc78f651e1d48db35638280722f8019798eccd6
  • Loading branch information
stephenfin committed Mar 21, 2022
1 parent 4983b90 commit fa137a5
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 175 deletions.
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@

requests>=2.4.0
six>=1.9.0
2 changes: 1 addition & 1 deletion swiftclient/authv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import json
import time

from six.moves.urllib.parse import urljoin
from urllib.parse import urljoin

# Note that while we import keystoneauth1 here, we *don't* need to add it to
# requirements.txt -- this entire module only makes sense (and should only be
Expand Down
59 changes: 21 additions & 38 deletions swiftclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
import warnings

from requests.exceptions import RequestException, SSLError
from six.moves import http_client
from six.moves.urllib.parse import quote as _quote, unquote
from six.moves.urllib.parse import urljoin, urlparse, urlunparse
import http.client as http_client
from urllib.parse import quote as _quote, unquote
from urllib.parse import urljoin, urlparse, urlunparse
from time import sleep, time
import six

from swiftclient import version as swiftclient_version
from swiftclient.exceptions import ClientException
Expand Down Expand Up @@ -165,34 +164,20 @@ def http_log(args, kwargs, resp, body):


def parse_header_string(data):
if not isinstance(data, (six.text_type, six.binary_type)):
if not isinstance(data, (str, bytes)):
data = str(data)
if six.PY2:
if isinstance(data, six.text_type):
# Under Python2 requests only returns binary_type, but if we get
# some stray text_type input, this should prevent unquote from
# interpreting %-encoded data as raw code-points.
data = data.encode('utf8')
if isinstance(data, bytes):
# Under Python3 requests only returns text_type and tosses (!) the
# rest of the headers. If that ever changes, this should be a sane
# approach.
try:
unquoted = unquote(data).decode('utf8')
data = data.decode('ascii')
except UnicodeDecodeError:
try:
return data.decode('utf8')
except UnicodeDecodeError:
return quote(data).decode('utf8')
else:
if isinstance(data, six.binary_type):
# Under Python3 requests only returns text_type and tosses (!) the
# rest of the headers. If that ever changes, this should be a sane
# approach.
try:
data = data.decode('ascii')
except UnicodeDecodeError:
data = quote(data)
try:
unquoted = unquote(data, errors='strict')
except UnicodeDecodeError:
return data
data = quote(data)
try:
unquoted = unquote(data, errors='strict')
except UnicodeDecodeError:
return data
return unquoted


Expand All @@ -201,20 +186,18 @@ def quote(value, safe='/'):
Patched version of urllib.quote that encodes utf8 strings before quoting.
On Python 3, call directly urllib.parse.quote().
"""
if six.PY3:
return _quote(value, safe=safe)
return _quote(encode_utf8(value), safe)
return _quote(value, safe=safe)


def encode_utf8(value):
if type(value) in six.integer_types + (float, bool):
if type(value) in (int, float, bool):
# As of requests 2.11.0, headers must be byte- or unicode-strings.
# Convert some known-good types as a convenience for developers.
# Note that we *don't* convert subclasses, as they may have overriddden
# __str__ or __repr__.
# See https://github.com/kennethreitz/requests/pull/3366 for more info
value = str(value)
if isinstance(value, six.text_type):
if isinstance(value, str):
value = value.encode('utf8')
return value

Expand All @@ -226,7 +209,7 @@ def encode_meta_headers(headers):
value = encode_utf8(value)
header = header.lower()

if (isinstance(header, six.string_types) and
if (isinstance(header, str) and
header.startswith(USER_METADATA_TYPE)):
header = encode_utf8(header)

Expand Down Expand Up @@ -457,12 +440,12 @@ def getresponse(self):
old_getheader = self.resp.raw.getheader

def _decode_header(string):
if string is None or six.PY2:
if string is None:
return string
return string.encode('iso-8859-1').decode('utf-8')

def _encode_header(string):
if string is None or six.PY2:
if string is None:
return string
return string.encode('utf-8').decode('iso-8859-1')

Expand Down Expand Up @@ -1441,7 +1424,7 @@ def put_object(url, token=None, container=None, name=None, contents=None,
warnings.warn(warn_msg, stacklevel=2)
# Match requests's is_stream test
if hasattr(contents, '__iter__') and not isinstance(contents, (
six.text_type, six.binary_type, list, tuple, dict)):
str, bytes, list, tuple, dict)):
contents = iter_wrapper(contents)
conn.request('PUT', path, contents, headers)

Expand Down
2 changes: 1 addition & 1 deletion swiftclient/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from six.moves import urllib
import urllib


class ClientException(Exception):
Expand Down
13 changes: 3 additions & 10 deletions swiftclient/multithreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import six
import sys

from concurrent.futures import ThreadPoolExecutor
from six.moves.queue import PriorityQueue
from queue import PriorityQueue


class OutputManager(object):
Expand Down Expand Up @@ -70,12 +69,8 @@ def print_raw(self, data):
self.print_pool.submit(self._write, data, self.print_stream)

def _write(self, data, stream):
if six.PY3:
stream.buffer.write(data)
stream.flush()
if six.PY2:
stream.write(data)
stream.flush()
stream.buffer.write(data)
stream.flush()

def print_msg(self, msg, *fmt_args):
if fmt_args:
Expand All @@ -100,8 +95,6 @@ def get_error_count(self):
def _print(self, item, stream=None):
if stream is None:
stream = self.print_stream
if six.PY2 and isinstance(item, six.text_type):
item = item.encode('utf8')
print(item, file=stream)

def _print_error(self, item, count=1):
Expand Down
30 changes: 15 additions & 15 deletions swiftclient/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from copy import deepcopy
from errno import EEXIST, ENOENT
from hashlib import md5
from io import StringIO
from os import environ, makedirs, stat, utime
from os.path import (
basename, dirname, getmtime, getsize, isdir, join, sep as os_path_sep
Expand All @@ -29,10 +30,9 @@
from random import shuffle
from time import time
from threading import Thread
from six import Iterator, StringIO, string_types, text_type
from six.moves.queue import Queue
from six.moves.queue import Empty as QueueEmpty
from six.moves.urllib.parse import quote
from queue import Queue
from queue import Empty as QueueEmpty
from urllib.parse import quote

import json

Expand All @@ -54,7 +54,7 @@
logger = logging.getLogger("swiftclient.service")


class ResultsIterator(Iterator):
class ResultsIterator:
def __init__(self, futures):
self.futures = interruptable_as_completed(futures)

Expand Down Expand Up @@ -321,10 +321,10 @@ class SwiftUploadObject(object):
options to be specified separately for each individual object.
"""
def __init__(self, source, object_name=None, options=None):
if isinstance(source, string_types):
if isinstance(source, str):
self.object_name = object_name or source
elif source is None or hasattr(source, 'read'):
if not object_name or not isinstance(object_name, string_types):
if not object_name or not isinstance(object_name, str):
raise SwiftError('Object names must be specified as '
'strings for uploads from None or file '
'like objects.')
Expand All @@ -347,7 +347,7 @@ class SwiftPostObject(object):
specified separately for each individual object.
"""
def __init__(self, object_name, options=None):
if not (isinstance(object_name, string_types) and object_name):
if not (isinstance(object_name, str) and object_name):
raise SwiftError(
"Object names must be specified as non-empty strings"
)
Expand All @@ -361,7 +361,7 @@ class SwiftDeleteObject(object):
specified separately for each individual object.
"""
def __init__(self, object_name, options=None):
if not (isinstance(object_name, string_types) and object_name):
if not (isinstance(object_name, str) and object_name):
raise SwiftError(
"Object names must be specified as non-empty strings"
)
Expand All @@ -377,7 +377,7 @@ class SwiftCopyObject(object):
destination and fresh_metadata should be set in options
"""
def __init__(self, object_name, options=None):
if not (isinstance(object_name, string_types) and object_name):
if not (isinstance(object_name, str) and object_name):
raise SwiftError(
"Object names must be specified as non-empty strings"
)
Expand Down Expand Up @@ -835,7 +835,7 @@ def _make_post_objects(objects):
post_objects = []

for o in objects:
if isinstance(o, string_types):
if isinstance(o, str):
obj = SwiftPostObject(o)
post_objects.append(obj)
elif isinstance(o, SwiftPostObject):
Expand Down Expand Up @@ -1637,7 +1637,7 @@ def _make_upload_objects(objects, pseudo_folder=''):
upload_objects = []

for o in objects:
if isinstance(o, string_types):
if isinstance(o, str):
obj = SwiftUploadObject(o, urljoin(pseudo_folder,
o.lstrip('/')))
upload_objects.append(obj)
Expand Down Expand Up @@ -2035,7 +2035,7 @@ def _upload_slo_manifest(conn, segment_results, container, obj, headers):
segment_results.sort(key=lambda di: di['segment_index'])
for seg in segment_results:
seg_loc = seg['segment_location'].lstrip('/')
if isinstance(seg_loc, text_type):
if isinstance(seg_loc, str):
seg_loc = seg_loc.encode('utf-8')

manifest_data = json.dumps([
Expand Down Expand Up @@ -2578,7 +2578,7 @@ def _make_delete_objects(objects):
delete_objects = []

for o in objects:
if isinstance(o, string_types):
if isinstance(o, str):
obj = SwiftDeleteObject(o)
delete_objects.append(obj)
elif isinstance(o, SwiftDeleteObject):
Expand Down Expand Up @@ -2933,7 +2933,7 @@ def _make_copy_objects(objects, options):
copy_objects = []

for o in objects:
if isinstance(o, string_types):
if isinstance(o, str):
obj = SwiftCopyObject(o, options)
copy_objects.append(obj)
elif isinstance(o, SwiftCopyObject):
Expand Down
9 changes: 2 additions & 7 deletions swiftclient/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

from os import environ, walk, _exit as os_exit
from os.path import isfile, isdir, join
from six import text_type, PY2
from six.moves.urllib.parse import unquote, urlparse
from urllib.parse import unquote, urlparse
from sys import argv as sys_argv, exit, stderr, stdin
from time import gmtime, strftime

Expand Down Expand Up @@ -191,10 +190,6 @@ def st_delete(parser, args, output_manager, return_parser=False):
for o, err in r.get('result', {}).get('Errors', []):
# o will be of the form quote("/<cont>/<obj>")
o = unquote(o)
if PY2:
# In PY3, unquote(unicode) uses utf-8 like we
# want, but PY2 uses latin-1
o = o.encode('latin-1').decode('utf-8')
output_manager.error('Error Deleting: {0}: {1}'
.format(o[1:], err))
try:
Expand Down Expand Up @@ -1931,7 +1926,7 @@ def add_default_args(parser):
def main(arguments=None):
argv = sys_argv if arguments is None else arguments

argv = [a if isinstance(a, text_type) else a.decode('utf-8') for a in argv]
argv = [a if isinstance(a, str) else a.decode('utf-8') for a in argv]

parser = argparse.ArgumentParser(
add_help=False, formatter_class=HelpFormatter, usage='''
Expand Down

0 comments on commit fa137a5

Please sign in to comment.