Skip to content

Commit de6554d

Browse files
authored
remove six dependency (#324)
remove six dependency This requires adjustments to exception handling in fixtures to properly re-raise them.
1 parent 8ee6cb7 commit de6554d

18 files changed

+35
-72
lines changed

gabbi/case.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
from collections import OrderedDict
2121
import copy
2222
import functools
23+
from http import cookies
2324
import os
2425
import re
2526
import sys
2627
import time
2728
import unittest
2829
from unittest import result as unitresult
30+
import urllib.parse as urlparse
2931

30-
import six
31-
from six.moves import http_cookies
32-
from six.moves.urllib import parse as urlparse
3332
import wsgi_intercept
3433

3534
from gabbi import __version__
@@ -298,10 +297,10 @@ def _cookie_replacer(self, match):
298297
referred_case = self.history[case]
299298
else:
300299
referred_case = self.prior
301-
response_cookies = referred_case.response['set-cookie']
302-
cookies = http_cookies.SimpleCookie()
303-
cookies.load(response_cookies)
304-
cookie_string = cookies.output(attrs=[], header='', sep=',').strip()
300+
response_cookie = referred_case.response['set-cookie']
301+
cookie = cookies.SimpleCookie()
302+
cookie.load(response_cookie)
303+
cookie_string = cookie.output(attrs=[], header='', sep=',').strip()
305304
return cookie_string
306305

307306
def _headers_replace(self, message, escape_regex=False):
@@ -482,7 +481,7 @@ def _response_replacer(self, match, preserve=False):
482481
return self._cast_value(result, match.string)
483482
return result
484483
else:
485-
return six.text_type(result)
484+
return str(result)
486485

487486
def _run_request(
488487
self,
@@ -514,8 +513,8 @@ def _run_request(
514513
)
515514
except wsgi_intercept.WSGIAppError as exc:
516515
# Extract and re-raise the wrapped exception.
517-
six.reraise(exc.exception_type, exc.exception_value,
518-
exc.traceback)
516+
raise (exc.exception_type, exc.exception_value,
517+
exc.traceback)
519518

520519
# Set headers and location attributes for follow on requests
521520
self.response = response
@@ -582,7 +581,7 @@ def _run_test(self):
582581

583582
# ensure body is bytes, encoding as UTF-8 because that's
584583
# what we do here
585-
if isinstance(body, six.text_type):
584+
if isinstance(body, str):
586585
body = body.encode('UTF-8')
587586

588587
if test['poll']:
@@ -637,10 +636,10 @@ def _test_data_to_string(self, data, content_type):
637636
"""
638637
dumper_class = self.get_content_handler(content_type)
639638
if not _is_complex_type(data):
640-
if isinstance(data, six.string_types) and data.startswith('<@'):
639+
if isinstance(data, str) and data.startswith('<@'):
641640
info = self.load_data_file(data.replace('<@', '', 1))
642641
if utils.not_binary(content_type):
643-
data = six.text_type(info, 'UTF-8')
642+
data = str(info, 'UTF-8')
644643
else:
645644
# Return early we are binary content
646645
return info
@@ -661,7 +660,7 @@ def _test_data_to_string(self, data, content_type):
661660

662661
# If the result after template handling is not a string, dump
663662
# it if there is a suitable dumper.
664-
if dumper_class and not isinstance(data, six.string_types):
663+
if dumper_class and not isinstance(data, str):
665664
# If there are errors dumping we want them to raise to the
666665
# test harness.
667666
data = dumper_class.dumps(data, test=self)

gabbi/fixture.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
import sys
1717
from unittest import case
1818

19-
import six
20-
2119

2220
class GabbiFixtureError(Exception):
2321
"""Generic exception for GabbiFixture."""
2422
pass
2523

2624

27-
class GabbiFixture(object):
25+
class GabbiFixture:
2826
"""A context manager that operates as a fixture.
2927
3028
Subclasses must implement ``start_fixture`` and ``stop_fixture``, each
@@ -97,4 +95,4 @@ def nest(fixtures):
9795
except Exception:
9896
exc = sys.exc_info()
9997
if exc != (None, None, None):
100-
six.reraise(exc[0], exc[1], exc[2])
98+
raise exc[1].with_traceback(exc[2])

gabbi/handlers/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from gabbi.exception import GabbiFormatError
1616

1717

18-
class ResponseHandler(object):
18+
class ResponseHandler:
1919
"""Add functionality for making assertions about an HTTP response.
2020
2121
A subclass may implement two methods: ``action`` and ``preprocess``.

gabbi/handlers/jsonhandler.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
import json
1616

17-
import six
18-
1917
from gabbi.exception import GabbiDataLoadError
2018
from gabbi.handlers import base
2119
from gabbi import json_parser
@@ -68,7 +66,7 @@ def loads(data):
6866
@staticmethod
6967
def load_data_file(test, file_path):
7068
info = test.load_data_file(file_path)
71-
info = six.text_type(info, 'UTF-8')
69+
info = str(info, 'UTF-8')
7270
return json.loads(info)
7371

7472
@staticmethod
@@ -103,7 +101,7 @@ def action(self, test, path, value=None):
103101
'%s' % (path, test.response_data))
104102

105103
# read data from disk if the value starts with '<@'
106-
if isinstance(value, six.string_types) and value.startswith('<@'):
104+
if isinstance(value, str) and value.startswith('<@'):
107105
# Do template expansion in the rhs if rhs_path is provided.
108106
if ':' in value:
109107
value, rhs_path = value.split(':$', 1)
@@ -120,7 +118,7 @@ def action(self, test, path, value=None):
120118
'match %s' % (rhs_path, value))
121119

122120
# If expected is a string, check to see if it is a regex.
123-
is_regex = (isinstance(value, six.string_types) and
121+
is_regex = (isinstance(value, str) and
124122
value.startswith('/') and
125123
value.endswith('/') and
126124
len(value) > 1)
@@ -130,7 +128,7 @@ def action(self, test, path, value=None):
130128
if is_regex and not rhs_match:
131129
expected = expected[1:-1]
132130
# match may be a number so stringify
133-
match = six.text_type(match)
131+
match = str(match)
134132
test.assertRegex(
135133
match, expected,
136134
'Expect jsonpath %s to match /%s/, got %s' %

gabbi/handlers/yaml_disk_loading_jsonhandler.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
import yaml
1616

17-
import six
18-
1917
from gabbi.handlers import jsonhandler
2018

2119

@@ -36,5 +34,5 @@ class YAMLDiskLoadingJSONHandler(jsonhandler.JSONHandler):
3634
@staticmethod
3735
def load_data_file(test, file_path):
3836
info = test.load_data_file(file_path)
39-
info = six.text_type(info, 'UTF-8')
37+
info = str(info, 'UTF-8')
4038
return yaml.safe_load(info)

gabbi/httpclient.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313

14-
from __future__ import print_function
15-
1614
import os
1715
import sys
1816

1917
import certifi
20-
import six
2118
import urllib3
2219

2320
from gabbi.handlers import jsonhandler
@@ -154,7 +151,7 @@ def _print_body(self, headers, content):
154151
content_type = utils.extract_content_type(headers, 'text/plain')[0]
155152
if self._show_body and utils.not_binary(content_type):
156153
content = utils.decode_response_content(headers, content)
157-
if isinstance(content, six.binary_type):
154+
if isinstance(content, bytes):
158155
content = content.decode('utf-8')
159156
# TODO(cdent): Using the JSONHandler here instead of
160157
# just the json module to make it clear that eventually

gabbi/runner.py

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# under the License.
1313
"""Implementation of a command-line runner for gabbi files (AKA suites)."""
1414

15-
from __future__ import print_function
16-
1715
import argparse
1816
from importlib import import_module
1917
import os

gabbi/suitemaker.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from gabbi import suite
2828

2929

30-
class TestMaker(object):
30+
class TestMaker:
3131
"""A class for encapsulating test invariants.
3232
3333
All of the tests in a single gabbi file have invariants which are

gabbi/tests/__init__.py

-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +0,0 @@
1-
#
2-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3-
# not use this file except in compliance with the License. You may obtain
4-
# a copy of the License at
5-
#
6-
# http://www.apache.org/licenses/LICENSE-2.0
7-
#
8-
# Unless required by applicable law or agreed to in writing, software
9-
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10-
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11-
# License for the specific language governing permissions and limitations
12-
# under the License.
13-
14-
import six
15-
16-
six.add_move(six.MovedModule('mock', 'mock', 'unittest.mock'))

gabbi/tests/simple_wsgi.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
"""
1717

1818
import json
19-
20-
from six.moves.urllib import parse as urlparse
19+
import urllib.parse as urlparse
2120

2221

2322
CURRENT_POLL = 0
2423
METHODS = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH']
2524

2625

27-
class SimpleWsgi(object):
26+
class SimpleWsgi:
2827
"""A simple wsgi application to use in tests."""
2928

3029
def __call__(self, environ, start_response):

gabbi/tests/test_fixtures.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"""
1515

1616
import unittest
17-
18-
from six.moves import mock
17+
from unittest import mock
1918

2019
from gabbi import fixture
2120

gabbi/tests/test_inner_fixture.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def stop_fixture(self):
4343
assert COUNT_OUTER == 1
4444

4545

46-
class InnerFixture(object):
46+
class InnerFixture:
4747
"""Test that setUp is called 3 times."""
4848

4949
def setUp(self):

gabbi/tests/test_load_data_file.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"""
1515

1616
import unittest
17-
18-
from six.moves import mock
17+
from unittest import mock
1918

2019
from gabbi import case
2120

gabbi/tests/test_runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"""Test that the CLI works as expected
1414
"""
1515

16+
from io import StringIO
1617
import sys
1718
import unittest
1819
from uuid import uuid4
1920

20-
from six import StringIO
2121
from wsgi_intercept.interceptor import Urllib3Interceptor
2222

2323
from gabbi import exception

gabbi/tests/test_suite.py

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from gabbi import suitemaker
2121

2222
VALUE_ERROR = 'value error sentinel'
23+
FIXTURE_METHOD = 'start_fixture'
2324

2425

2526
class FakeFixture(fixture.GabbiFixture):
@@ -54,3 +55,4 @@ def test_suite_catches_fixture_fail(self):
5455

5556
self.assertIn('foo_alpha', str(errored_test))
5657
self.assertIn(VALUE_ERROR, trace)
58+
self.assertIn(FIXTURE_METHOD, trace)

gabbi/tests/test_use_prior_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
import copy
1717
import unittest
18-
19-
from six.moves import mock
18+
from unittest import mock
2019

2120
from gabbi import case
2221

gabbi/utils.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@
1414

1515
import io
1616
import os
17+
import urllib.parse as urlparse
1718

1819
import colorama
19-
import six
20-
from six.moves.urllib import parse as urlparse
2120
import yaml
2221

23-
24-
try: # Python 3
25-
ConnectionRefused = ConnectionRefusedError
26-
except NameError: # Python 2
27-
import socket
28-
ConnectionRefused = socket.error
22+
ConnectionRefused = ConnectionRefusedError
2923

3024

3125
def create_url(base_url, host, port=None, prefix='', ssl=False):
@@ -72,7 +66,7 @@ def decode_response_content(header_dict, content):
7266
"""Decode content to a proper string."""
7367
content_type, charset = extract_content_type(header_dict)
7468

75-
if not_binary(content_type) and isinstance(content, six.binary_type):
69+
if not_binary(content_type) and isinstance(content, bytes):
7670
return content.decode(charset)
7771
else:
7872
return content

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pbr
22
pytest
3-
six
43
PyYAML
54
urllib3>=1.26.9,<2.0.0
65
certifi

0 commit comments

Comments
 (0)