diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 2caa6ff..b801c63 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -20,7 +20,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pytest coverage pytest-cov six mock + pip install pytest coverage pytest-cov mock if [ -f requirements.txt ]; then pip install -r requirements.txt; fi pip install -e . - name: Test with pytest @@ -46,7 +46,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pytest coverage pytest-cov six mock + pip install pytest coverage pytest-cov mock if [ -f requirements.txt ]; then pip install -r requirements.txt; fi pip install -e . - name: Test with pytest diff --git a/setup.py b/setup.py index b9e61b5..8fb478f 100755 --- a/setup.py +++ b/setup.py @@ -40,10 +40,6 @@ DESCRIPTION = 'Config file reading, writing and validation.' URL = 'https://github.com/DiffSK/configobj' -REQUIRES = """ - six -""" - VERSION = '' with closing(open(os.path.join(__here__, 'src', PACKAGES[0], '_version.py'), 'r')) as handle: for line in handle.readlines(): @@ -120,7 +116,6 @@ py_modules=MODULES, package_dir={'': 'src'}, packages=PACKAGES, - install_requires=[i.strip() for i in REQUIRES.splitlines() if i.strip()], python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*', classifiers=CLASSIFIERS, keywords=KEYWORDS, diff --git a/src/configobj/__init__.py b/src/configobj/__init__.py index 0580b65..2515d9f 100644 --- a/src/configobj/__init__.py +++ b/src/configobj/__init__.py @@ -31,7 +31,6 @@ # Python 2.7 from collections import Mapping -import six from ._version import __version__ # imported lazily to avoid startup performance hit if it isn't used @@ -507,11 +506,11 @@ def __getitem__(self, key): """Fetch the item and do string interpolation.""" val = dict.__getitem__(self, key) if self.main.interpolation: - if isinstance(val, six.string_types): + if isinstance(val, (str, )): return self._interpolate(key, val) if isinstance(val, list): def _check(entry): - if isinstance(entry, six.string_types): + if isinstance(entry, (str, )): return self._interpolate(key, entry) return entry new = [_check(entry) for entry in val] @@ -534,7 +533,7 @@ def __setitem__(self, key, value, unrepr=False): ``unrepr`` must be set when setting a value to a dictionary, without creating a new sub-section. """ - if not isinstance(key, six.string_types): + if not isinstance(key, (str, )): raise ValueError('The key "%s" is not a string.' % key) # add the comment @@ -568,11 +567,11 @@ def __setitem__(self, key, value, unrepr=False): if key not in self: self.scalars.append(key) if not self.main.stringify: - if isinstance(value, six.string_types): + if isinstance(value, (str, )): pass elif isinstance(value, (list, tuple)): for entry in value: - if not isinstance(entry, six.string_types): + if not isinstance(entry, (str, )): raise TypeError('Value is not a string "%s".' % entry) else: raise TypeError('Value is not a string "%s".' % value) @@ -920,7 +919,7 @@ def as_bool(self, key): return False else: try: - if not isinstance(val, six.string_types): + if not isinstance(val, (str, )): # TODO: Why do we raise a KeyError here? raise KeyError() else: @@ -1210,7 +1209,7 @@ def _load(self, infile, configspec): except AttributeError: pass - if isinstance(infile, six.string_types): + if isinstance(infile, (str, )): self.filename = infile if os.path.isfile(infile): with open(infile, 'rb') as h: @@ -1278,7 +1277,7 @@ def set_section(in_section, this_section): break break - assert all(isinstance(line, six.string_types) for line in content), repr(content) + assert all(isinstance(line, (str, )) for line in content), repr(content) content = [line.rstrip('\r\n') for line in content] self._parse(content) @@ -1387,7 +1386,7 @@ def _handle_bom(self, infile): else: line = infile - if isinstance(line, six.text_type): + if isinstance(line, str): # it's already decoded and there's no need to do anything # else, just use the _decode utility method to handle # listifying appropriately @@ -1432,7 +1431,7 @@ def _handle_bom(self, infile): # No encoding specified - so we need to check for UTF8/UTF16 for BOM, (encoding, final_encoding) in list(BOMS.items()): - if not isinstance(line, six.binary_type) or not line.startswith(BOM): + if not isinstance(line, bytes) or not line.startswith(BOM): # didn't specify a BOM, or it's not a bytestring continue else: @@ -1448,9 +1447,9 @@ def _handle_bom(self, infile): else: infile = newline # UTF-8 - if isinstance(infile, six.text_type): + if isinstance(infile, str): return infile.splitlines(True) - elif isinstance(infile, six.binary_type): + elif isinstance(infile, bytes): return infile.decode('utf-8').splitlines(True) else: return self._decode(infile, 'utf-8') @@ -1458,12 +1457,8 @@ def _handle_bom(self, infile): return self._decode(infile, encoding) - if six.PY2 and isinstance(line, str): - # don't actually do any decoding, since we're on python 2 and - # returning a bytestring is fine - return self._decode(infile, None) # No BOM discovered and no encoding specified, default to UTF-8 - if isinstance(infile, six.binary_type): + if isinstance(infile, bytes): return infile.decode('utf-8').splitlines(True) else: return self._decode(infile, 'utf-8') @@ -1475,18 +1470,18 @@ def _decode(self, infile, encoding): if is a string, it also needs converting to a list. """ - if isinstance(infile, six.binary_type): + if isinstance(infile, bytes): # NOTE: Could raise a ``UnicodeDecodeError`` if encoding: return infile.decode(encoding).splitlines(True) else: return infile.splitlines(True) - if isinstance(infile, six.string_types): + if isinstance(infile, (str, )): return infile.splitlines(True) if encoding: for i, line in enumerate(infile): - if isinstance(line, six.binary_type): + if isinstance(line, bytes): # NOTE: The isinstance test here handles mixed lists of unicode/string # NOTE: But the decode will break on any non-string values # NOTE: Or could raise a ``UnicodeDecodeError`` @@ -1496,7 +1491,7 @@ def _decode(self, infile, encoding): def _decode_element(self, line): """Decode element to unicode if necessary.""" - if isinstance(line, six.binary_type) and self.default_encoding: + if isinstance(line, bytes) and self.default_encoding: return line.decode(self.default_encoding) else: return line @@ -1508,7 +1503,7 @@ def _str(self, value): Used by ``stringify`` within validate, to turn non-string values into strings. """ - if not isinstance(value, six.string_types): + if not isinstance(value, (str, )): # intentionally 'str' because it's just whatever the "normal" # string type is for the python version we're dealing with return str(value) @@ -1761,7 +1756,7 @@ def _quote(self, value, multiline=True): return self._quote(value[0], multiline=False) + ',' return ', '.join([self._quote(val, multiline=False) for val in value]) - if not isinstance(value, six.string_types): + if not isinstance(value, (str, )): if self.stringify: # intentionally 'str' because it's just whatever the "normal" # string type is for the python version we're dealing with @@ -2081,7 +2076,7 @@ def write(self, outfile=None, section=None): if not output.endswith(newline): output += newline - if isinstance(output, six.binary_type): + if isinstance(output, bytes): output_bytes = output else: output_bytes = output.encode(self.encoding or @@ -2323,7 +2318,7 @@ def reload(self): This method raises a ``ReloadError`` if the ConfigObj doesn't have a filename attribute pointing to a file. """ - if not isinstance(self.filename, six.string_types): + if not isinstance(self.filename, (str, )): raise ReloadError() filename = self.filename diff --git a/src/configobj/validate.py b/src/configobj/validate.py index 5775aed..25b8779 100644 --- a/src/configobj/validate.py +++ b/src/configobj/validate.py @@ -167,22 +167,6 @@ ) -#TODO - #21 - six is part of the repo now, but we didn't switch over to it here -# this could be replaced if six is used for compatibility, or there are no -# more assertions about items being a string -if sys.version_info < (3,): - string_type = basestring -else: - string_type = str - # so tests that care about unicode on 2.x can specify unicode, and the same - # tests when run on 3.x won't complain about a undefined name "unicode" - # since all strings are unicode on 3.x we just want to pass it through - # unchanged - unicode = lambda x: x - # in python 3, all ints are equivalent to python 2 longs, and they'll - # never show "L" in the repr - long = int - _list_arg = re.compile(r''' (?: ([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*list\( @@ -284,22 +268,22 @@ def dottedQuadToNum(ip): def numToDottedQuad(num): """ - Convert int or long int to dotted quad string + Convert int int to dotted quad string - >>> numToDottedQuad(long(-1)) + >>> numToDottedQuad(int(-1)) Traceback (most recent call last): ValueError: Not a good numeric IP: -1 - >>> numToDottedQuad(long(1)) + >>> numToDottedQuad(int(1)) '0.0.0.1' - >>> numToDottedQuad(long(16777218)) + >>> numToDottedQuad(int(16777218)) '1.0.0.2' - >>> numToDottedQuad(long(16908291)) + >>> numToDottedQuad(int(16908291)) '1.2.0.3' - >>> numToDottedQuad(long(16909060)) + >>> numToDottedQuad(int(16909060)) '1.2.3.4' - >>> numToDottedQuad(long(4294967295)) + >>> numToDottedQuad(int(4294967295)) '255.255.255.255' - >>> numToDottedQuad(long(4294967296)) + >>> numToDottedQuad(int(4294967296)) Traceback (most recent call last): ValueError: Not a good numeric IP: 4294967296 >>> numToDottedQuad(-1) @@ -325,11 +309,11 @@ def numToDottedQuad(num): import socket, struct # no need to intercept here, 4294967295L is fine - if num > long(4294967295) or num < 0: + if num > int(4294967295) or num < 0: raise ValueError('Not a good numeric IP: %s' % num) try: return socket.inet_ntoa( - struct.pack('!L', long(num))) + struct.pack('!L', int(num))) except (socket.error, struct.error, OverflowError): raise ValueError('Not a good numeric IP: %s' % num) @@ -481,9 +465,9 @@ class Validator(object): ... # check that value is of the correct type. ... # possible valid inputs are integers or strings ... # that represent integers - ... if not isinstance(value, (int, long, string_type)): + ... if not isinstance(value, (int, str)): ... raise VdtTypeError(value) - ... elif isinstance(value, string_type): + ... elif isinstance(value, str): ... # if we are given a string ... # attempt to convert to an integer ... try: @@ -754,7 +738,7 @@ def _is_num_param(names, values, to_float=False): for (name, val) in zip(names, values): if val is None: out_params.append(val) - elif isinstance(val, (int, long, float, string_type)): + elif isinstance(val, (int, float, str)): try: out_params.append(fun(val)) except ValueError: @@ -772,7 +756,7 @@ def _is_num_param(names, values, to_float=False): def is_integer(value, min=None, max=None): """ - A check that tests that a given value is an integer (int, or long) + A check that tests that a given value is an integer (int) and optionally, between bounds. A negative value is accepted, while a float will fail. @@ -812,9 +796,9 @@ def is_integer(value, min=None, max=None): """ (min_val, max_val) = _is_num_param( # pylint: disable=unbalanced-tuple-unpacking ('min', 'max'), (min, max)) - if not isinstance(value, (int, long, string_type)): + if not isinstance(value, (int, str)): raise VdtTypeError(value) - if isinstance(value, string_type): + if isinstance(value, str): # if it's a string - does it represent an integer ? try: value = int(value) @@ -864,7 +848,7 @@ def is_float(value, min=None, max=None): """ (min_val, max_val) = _is_num_param( # pylint: disable=unbalanced-tuple-unpacking ('min', 'max'), (min, max), to_float=True) - if not isinstance(value, (int, long, float, string_type)): + if not isinstance(value, (int, float, str)): raise VdtTypeError(value) if not isinstance(value, float): # if it's a string - does it represent a float ? @@ -929,7 +913,7 @@ def is_boolean(value): VdtTypeError: the value "up" is of the wrong type. """ - if isinstance(value, string_type): + if isinstance(value, str): try: return bool_dict[value.lower()] except KeyError: @@ -972,7 +956,7 @@ def is_ip_addr(value): Traceback (most recent call last): VdtTypeError: the value "0" is of the wrong type. """ - if not isinstance(value, string_type): + if not isinstance(value, str): raise VdtTypeError(value) value = value.strip() try: @@ -1015,7 +999,7 @@ def is_list(value, min=None, max=None): """ (min_len, max_len) = _is_num_param( # pylint: disable=unbalanced-tuple-unpacking ('min', 'max'), (min, max)) - if isinstance(value, string_type): + if isinstance(value, str): raise VdtTypeError(value) try: num_members = len(value) @@ -1084,7 +1068,7 @@ def is_string(value, min=None, max=None): Traceback (most recent call last): VdtValueTooLongError: the value "1234" is too long. """ - if not isinstance(value, string_type): + if not isinstance(value, str): raise VdtTypeError(value) (min_len, max_len) = _is_num_param( # pylint: disable=unbalanced-tuple-unpacking ('min', 'max'), (min, max)) @@ -1191,7 +1175,7 @@ def is_string_list(value, min=None, max=None): Traceback (most recent call last): VdtTypeError: the value "hello" is of the wrong type. """ - if isinstance(value, string_type): + if isinstance(value, str): raise VdtTypeError(value) return [is_string(mem) for mem in is_list(value, min, max)] @@ -1326,7 +1310,7 @@ def is_option(value, *options): Traceback (most recent call last): VdtTypeError: the value "0" is of the wrong type. """ - if not isinstance(value, string_type): + if not isinstance(value, str): raise VdtTypeError(value) if not value in options: raise VdtValueError(value) @@ -1398,15 +1382,15 @@ def _test(value, *args, **keywargs): >>> v.check('pass(default=list(1, 2, 3, 4))', None, True) ['1', '2', '3', '4'] - Bug test for unicode arguments + Bug test for str arguments >>> v = Validator() - >>> v.check(unicode('string(min=4)'), unicode('test')) == unicode('test') + >>> v.check('string(min=4)', 'test') == 'test' True >>> v = Validator() - >>> v.get_default_value(unicode('string(min=4, default="1234")')) == unicode('1234') + >>> v.get_default_value('string(min=4, default="1234")') == '1234' True - >>> v.check(unicode('string(min=4, default="1234")'), unicode('test')) == unicode('test') + >>> v.check('string(min=4, default="1234")', 'test') == 'test' True >>> v = Validator() diff --git a/src/tests/test_configobj.py b/src/tests/test_configobj.py index b1f2d01..78af9ed 100644 --- a/src/tests/test_configobj.py +++ b/src/tests/test_configobj.py @@ -11,7 +11,7 @@ from tempfile import NamedTemporaryFile import pytest -import six +import io import mock import configobj as co @@ -40,13 +40,13 @@ def cfg_lines(config_string_representation): '{!r}'.format(config_string_representation)) first_content = lines[line_no_with_content] - if isinstance(first_content, six.binary_type): + if isinstance(first_content, bytes): first_content = first_content.decode('utf-8') ws_chars = len(re.search(r'^(\s*)', first_content).group(1)) def yield_stringified_line(): for line in lines: - if isinstance(line, six.binary_type): + if isinstance(line, bytes): yield line.decode('utf-8') else: yield line @@ -74,7 +74,7 @@ def make_file_with_contents_and_return_name(config_string_representation): with NamedTemporaryFile(delete=False, mode='wb') as cfg_file: for line in lines: - if isinstance(line, six.binary_type): + if isinstance(line, bytes): cfg_file.write(line + os.linesep.encode('utf-8')) else: cfg_file.write((line + os.linesep).encode('utf-8')) @@ -191,11 +191,7 @@ def test_unicode_conversion_when_encoding_is_set(self, cfg_contents): c = ConfigObj(cfg, encoding='utf8') - if six.PY2: - assert not isinstance(c['test'], str) - assert isinstance(c['test'], unicode) - else: - assert isinstance(c['test'], str) + assert isinstance(c['test'], str) #issue #18 @@ -203,11 +199,7 @@ def test_no_unicode_conversion_when_encoding_is_omitted(self, cfg_contents): cfg = cfg_contents(b"test = some string") c = ConfigObj(cfg) - if six.PY2: - assert isinstance(c['test'], str) - assert not isinstance(c['test'], unicode) - else: - assert isinstance(c['test'], str) + assert isinstance(c['test'], str) #issue #44 def test_that_encoding_using_list_of_strings(self): @@ -215,11 +207,7 @@ def test_that_encoding_using_list_of_strings(self): c = ConfigObj(cfg, encoding='utf8') - if six.PY2: - assert isinstance(c['test'], unicode) - assert not isinstance(c['test'], str) - else: - assert isinstance(c['test'], str) + assert isinstance(c['test'], str) assert c['test'] == '\U0001f41c' @@ -228,7 +216,7 @@ def test_encoding_in_subsections(self, ant_cfg, cfg_contents): c = cfg_contents(ant_cfg) cfg = ConfigObj(c, encoding='utf-8') - assert isinstance(cfg['tags']['bug']['translated'], six.text_type) + assert isinstance(cfg['tags']['bug']['translated'], str) #issue #44 and #55 def test_encoding_in_config_files(self, request, ant_cfg): @@ -238,7 +226,7 @@ def test_encoding_in_config_files(self, request, ant_cfg): request.addfinalizer(lambda : os.unlink(cfg_file.name)) cfg = ConfigObj(cfg_file.name, encoding='utf-8') - assert isinstance(cfg['tags']['bug']['translated'], six.text_type) + assert isinstance(cfg['tags']['bug']['translated'], str) cfg.write() def test_encoding_from_filelike(self): @@ -248,7 +236,7 @@ def test_encoding_from_filelike(self): c = ConfigObj(stream, encoding='utf-8') text = c['text'] - assert isinstance(text, unicode if six.PY2 else str) + assert isinstance(text, str) assert text == '\u00a7' @@ -516,7 +504,7 @@ def test_unicode_handling(): 'section': {'test': 'test', 'test2': 'test2'}} uc = ConfigObj(u, encoding='utf_8', default_encoding='latin-1') assert uc.BOM - assert isinstance(uc['test1'], six.text_type) + assert isinstance(uc['test1'], str) assert uc.encoding == 'utf_8' assert uc.newlines == '\n' assert len(uc.write()) == 13 @@ -524,14 +512,14 @@ def test_unicode_handling(): a_list = uc.write() assert 'latin1' in str(a_list) assert len(a_list) == 14 - assert isinstance(a_list[0], six.binary_type) + assert isinstance(a_list[0], bytes) assert a_list[0].startswith(BOM_UTF8) u = u_base.replace('\n', '\r\n').encode('utf-8').splitlines(True) uc = ConfigObj(u) assert uc.newlines == '\r\n' uc.newlines = '\r' - file_like = six.BytesIO() + file_like = io.BytesIO() uc.write(file_like) file_like.seek(0) uc2 = ConfigObj(file_like) @@ -739,7 +727,7 @@ def transform(section, key): val = section[key] newkey = key.replace('XXXX', 'CLIENT1') section.rename(key, newkey) - if isinstance(val, six.string_types): + if isinstance(val, (str, )): val = val.replace('XXXX', 'CLIENT1') section[newkey] = val @@ -827,7 +815,7 @@ def reloadable_cfg_content(self): return content def test_handle_no_filename(self): - for bad_args in ([six.BytesIO()], [], [[]]): + for bad_args in ([io.BytesIO()], [], [[]]): cfg = ConfigObj(*bad_args) with pytest.raises(ReloadError) as excinfo: cfg.reload() @@ -1044,11 +1032,11 @@ def test_triple_quote_newline_roundtrip(self): initial_conf['single'] = "single triple '''\n" initial_conf['double'] = 'double triple """\n' - io = six.BytesIO() - initial_conf.write(outfile=io) - io.seek(0) + f = io.BytesIO() + initial_conf.write(outfile=f) + f.seek(0) - loaded_conf = ConfigObj(io) + loaded_conf = ConfigObj(f) assert loaded_conf['single'] == "single triple '''\n" assert loaded_conf['double'] == 'double triple """\n' @@ -1380,21 +1368,21 @@ class TestEdgeCasesWhenWritingOut(object): def test_newline_terminated(self, empty_cfg): empty_cfg.newlines = '\n' empty_cfg['a'] = 'b' - collector = six.BytesIO() + collector = io.BytesIO() empty_cfg.write(collector) assert collector.getvalue() == b'a = b\n' def test_hash_escaping(self, empty_cfg): empty_cfg.newlines = '\n' empty_cfg['#a'] = 'b # something' - collector = six.BytesIO() + collector = io.BytesIO() empty_cfg.write(collector) assert collector.getvalue() == b'"#a" = "b # something"\n' empty_cfg = ConfigObj() empty_cfg.newlines = '\n' empty_cfg['a'] = 'b # something', 'c # something' - collector = six.BytesIO() + collector = io.BytesIO() empty_cfg.write(collector) assert collector.getvalue() == b'a = "b # something", "c # something"\n' diff --git a/tox.ini b/tox.ini index 1610a48..12a1d16 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,6 @@ envlist = py27, py34, py35, py36, py37 [testenv] deps = - six mock pytest pytest-cov