Skip to content

Commit

Permalink
remove six dependency (#324)
Browse files Browse the repository at this point in the history
remove six dependency

This requires adjustments to exception handling in
fixtures to properly re-raise them.
  • Loading branch information
a-detiste authored Oct 2, 2023
1 parent 8ee6cb7 commit de6554d
Show file tree
Hide file tree
Showing 18 changed files with 35 additions and 72 deletions.
27 changes: 13 additions & 14 deletions gabbi/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
from collections import OrderedDict
import copy
import functools
from http import cookies
import os
import re
import sys
import time
import unittest
from unittest import result as unitresult
import urllib.parse as urlparse

import six
from six.moves import http_cookies
from six.moves.urllib import parse as urlparse
import wsgi_intercept

from gabbi import __version__
Expand Down Expand Up @@ -298,10 +297,10 @@ def _cookie_replacer(self, match):
referred_case = self.history[case]
else:
referred_case = self.prior
response_cookies = referred_case.response['set-cookie']
cookies = http_cookies.SimpleCookie()
cookies.load(response_cookies)
cookie_string = cookies.output(attrs=[], header='', sep=',').strip()
response_cookie = referred_case.response['set-cookie']
cookie = cookies.SimpleCookie()
cookie.load(response_cookie)
cookie_string = cookie.output(attrs=[], header='', sep=',').strip()
return cookie_string

def _headers_replace(self, message, escape_regex=False):
Expand Down Expand Up @@ -482,7 +481,7 @@ def _response_replacer(self, match, preserve=False):
return self._cast_value(result, match.string)
return result
else:
return six.text_type(result)
return str(result)

def _run_request(
self,
Expand Down Expand Up @@ -514,8 +513,8 @@ def _run_request(
)
except wsgi_intercept.WSGIAppError as exc:
# Extract and re-raise the wrapped exception.
six.reraise(exc.exception_type, exc.exception_value,
exc.traceback)
raise (exc.exception_type, exc.exception_value,
exc.traceback)

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

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

if test['poll']:
Expand Down Expand Up @@ -637,10 +636,10 @@ def _test_data_to_string(self, data, content_type):
"""
dumper_class = self.get_content_handler(content_type)
if not _is_complex_type(data):
if isinstance(data, six.string_types) and data.startswith('<@'):
if isinstance(data, str) and data.startswith('<@'):
info = self.load_data_file(data.replace('<@', '', 1))
if utils.not_binary(content_type):
data = six.text_type(info, 'UTF-8')
data = str(info, 'UTF-8')
else:
# Return early we are binary content
return info
Expand All @@ -661,7 +660,7 @@ def _test_data_to_string(self, data, content_type):

# If the result after template handling is not a string, dump
# it if there is a suitable dumper.
if dumper_class and not isinstance(data, six.string_types):
if dumper_class and not isinstance(data, str):
# If there are errors dumping we want them to raise to the
# test harness.
data = dumper_class.dumps(data, test=self)
Expand Down
6 changes: 2 additions & 4 deletions gabbi/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
import sys
from unittest import case

import six


class GabbiFixtureError(Exception):
"""Generic exception for GabbiFixture."""
pass


class GabbiFixture(object):
class GabbiFixture:
"""A context manager that operates as a fixture.
Subclasses must implement ``start_fixture`` and ``stop_fixture``, each
Expand Down Expand Up @@ -97,4 +95,4 @@ def nest(fixtures):
except Exception:
exc = sys.exc_info()
if exc != (None, None, None):
six.reraise(exc[0], exc[1], exc[2])
raise exc[1].with_traceback(exc[2])
2 changes: 1 addition & 1 deletion gabbi/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from gabbi.exception import GabbiFormatError


class ResponseHandler(object):
class ResponseHandler:
"""Add functionality for making assertions about an HTTP response.
A subclass may implement two methods: ``action`` and ``preprocess``.
Expand Down
10 changes: 4 additions & 6 deletions gabbi/handlers/jsonhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import json

import six

from gabbi.exception import GabbiDataLoadError
from gabbi.handlers import base
from gabbi import json_parser
Expand Down Expand Up @@ -68,7 +66,7 @@ def loads(data):
@staticmethod
def load_data_file(test, file_path):
info = test.load_data_file(file_path)
info = six.text_type(info, 'UTF-8')
info = str(info, 'UTF-8')
return json.loads(info)

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

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

# If expected is a string, check to see if it is a regex.
is_regex = (isinstance(value, six.string_types) and
is_regex = (isinstance(value, str) and
value.startswith('/') and
value.endswith('/') and
len(value) > 1)
Expand All @@ -130,7 +128,7 @@ def action(self, test, path, value=None):
if is_regex and not rhs_match:
expected = expected[1:-1]
# match may be a number so stringify
match = six.text_type(match)
match = str(match)
test.assertRegex(
match, expected,
'Expect jsonpath %s to match /%s/, got %s' %
Expand Down
4 changes: 1 addition & 3 deletions gabbi/handlers/yaml_disk_loading_jsonhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import yaml

import six

from gabbi.handlers import jsonhandler


Expand All @@ -36,5 +34,5 @@ class YAMLDiskLoadingJSONHandler(jsonhandler.JSONHandler):
@staticmethod
def load_data_file(test, file_path):
info = test.load_data_file(file_path)
info = six.text_type(info, 'UTF-8')
info = str(info, 'UTF-8')
return yaml.safe_load(info)
5 changes: 1 addition & 4 deletions gabbi/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import print_function

import os
import sys

import certifi
import six
import urllib3

from gabbi.handlers import jsonhandler
Expand Down Expand Up @@ -154,7 +151,7 @@ def _print_body(self, headers, content):
content_type = utils.extract_content_type(headers, 'text/plain')[0]
if self._show_body and utils.not_binary(content_type):
content = utils.decode_response_content(headers, content)
if isinstance(content, six.binary_type):
if isinstance(content, bytes):
content = content.decode('utf-8')
# TODO(cdent): Using the JSONHandler here instead of
# just the json module to make it clear that eventually
Expand Down
2 changes: 0 additions & 2 deletions gabbi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# under the License.
"""Implementation of a command-line runner for gabbi files (AKA suites)."""

from __future__ import print_function

import argparse
from importlib import import_module
import os
Expand Down
2 changes: 1 addition & 1 deletion gabbi/suitemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from gabbi import suite


class TestMaker(object):
class TestMaker:
"""A class for encapsulating test invariants.
All of the tests in a single gabbi file have invariants which are
Expand Down
16 changes: 0 additions & 16 deletions gabbi/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +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 six

six.add_move(six.MovedModule('mock', 'mock', 'unittest.mock'))
5 changes: 2 additions & 3 deletions gabbi/tests/simple_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
"""

import json

from six.moves.urllib import parse as urlparse
import urllib.parse as urlparse


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


class SimpleWsgi(object):
class SimpleWsgi:
"""A simple wsgi application to use in tests."""

def __call__(self, environ, start_response):
Expand Down
3 changes: 1 addition & 2 deletions gabbi/tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"""

import unittest

from six.moves import mock
from unittest import mock

from gabbi import fixture

Expand Down
2 changes: 1 addition & 1 deletion gabbi/tests/test_inner_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def stop_fixture(self):
assert COUNT_OUTER == 1


class InnerFixture(object):
class InnerFixture:
"""Test that setUp is called 3 times."""

def setUp(self):
Expand Down
3 changes: 1 addition & 2 deletions gabbi/tests/test_load_data_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"""

import unittest

from six.moves import mock
from unittest import mock

from gabbi import case

Expand Down
2 changes: 1 addition & 1 deletion gabbi/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"""Test that the CLI works as expected
"""

from io import StringIO
import sys
import unittest
from uuid import uuid4

from six import StringIO
from wsgi_intercept.interceptor import Urllib3Interceptor

from gabbi import exception
Expand Down
2 changes: 2 additions & 0 deletions gabbi/tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from gabbi import suitemaker

VALUE_ERROR = 'value error sentinel'
FIXTURE_METHOD = 'start_fixture'


class FakeFixture(fixture.GabbiFixture):
Expand Down Expand Up @@ -54,3 +55,4 @@ def test_suite_catches_fixture_fail(self):

self.assertIn('foo_alpha', str(errored_test))
self.assertIn(VALUE_ERROR, trace)
self.assertIn(FIXTURE_METHOD, trace)
3 changes: 1 addition & 2 deletions gabbi/tests/test_use_prior_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import copy
import unittest

from six.moves import mock
from unittest import mock

from gabbi import case

Expand Down
12 changes: 3 additions & 9 deletions gabbi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@

import io
import os
import urllib.parse as urlparse

import colorama
import six
from six.moves.urllib import parse as urlparse
import yaml


try: # Python 3
ConnectionRefused = ConnectionRefusedError
except NameError: # Python 2
import socket
ConnectionRefused = socket.error
ConnectionRefused = ConnectionRefusedError


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

if not_binary(content_type) and isinstance(content, six.binary_type):
if not_binary(content_type) and isinstance(content, bytes):
return content.decode(charset)
else:
return content
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pbr
pytest
six
PyYAML
urllib3>=1.26.9,<2.0.0
certifi
Expand Down

0 comments on commit de6554d

Please sign in to comment.