Changeset 1721:3557f65ded13


Ignore:
Timestamp:
Dec 26, 2018, 3:22:08 PM (7 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Remove `compat.py`

This module was for Python 2/3 compatibility, similar to the `six`
package. Since the next version of ftputil will only support Python 3,
hardcode the types from `compat.py` in the code that used to use the
`compat` module.

There may still be redundant code that isn't needed when running the
tests under Python 3.
Files:
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • ftputil/file.py

    r1718 r1721  
    99import io
    1010
    11 import ftputil.compat
    1211import ftputil.error
    1312
  • ftputil/path.py

    r1718 r1721  
    1010import stat
    1111
    12 import ftputil.compat
    1312import ftputil.error
    1413import ftputil.tool
     
    9695        # These checks are implicitly done by Python 3, but not by
    9796        # Python 2.
    98         all_paths_are_unicode = all(
    99           (isinstance(path, ftputil.compat.unicode_type)
    100           for path in paths))
    101         all_paths_are_bytes = all(
    102           (isinstance(path, ftputil.compat.bytes_type)
    103           for path in paths))
     97        all_paths_are_unicode = all((isinstance(path, str)
     98                                     for path in paths))
     99        all_paths_are_bytes = all((isinstance(path, bytes)
     100                                   for path in paths))
    104101        if all_paths_are_unicode or all_paths_are_bytes:
    105102            return posixpath.join(*paths)
  • ftputil/tool.py

    r1713 r1721  
    66tool.py - helper code
    77"""
    8 
    9 import ftputil.compat as compat
    10 
    118
    129__all__ = ["same_string_type_as", "as_bytes", "as_unicode",
     
    3431    """
    3532    if (
    36       isinstance(type_source, compat.bytes_type) and
    37       isinstance(content_source, compat.unicode_type)):
     33      isinstance(type_source, bytes) and isinstance(content_source, str)):
    3834        return content_source.encode(LOSSLESS_ENCODING)
    3935    elif (
    40       isinstance(type_source, compat.unicode_type) and
    41       isinstance(content_source, compat.bytes_type)):
     36      isinstance(type_source, str) and isinstance(content_source, bytes)):
    4237        return content_source.decode(LOSSLESS_ENCODING)
    4338    else:
     
    6762    `LOSSLESS_ENCODING` is used for encoding or decoding.
    6863    """
    69     return same_string_type_as(compat.default_string_type(), string)
     64    return same_string_type_as("", string)
    7065
    7166
     
    7570    a unicode string. Otherwise return `string` unchanged.
    7671    """
    77     if isinstance(string, compat.unicode_type):
     72    if isinstance(string, str):
    7873        return string.encode(encoding)
    7974    else:
  • test/test_file.py

    r1718 r1721  
    77import pytest
    88
    9 import ftputil.compat
    109import ftputil.error
    1110
     
    2423class ReadMockSessionWithMoreNewlines(mock_ftplib.MockSession):
    2524
    26     mock_file_content = b"\r\n".join(map(ftputil.compat.bytes_type, range(20)))
     25    mock_file_content = b"\r\n".join(map(bytes, range(20)))
    2726
    2827
  • test/test_file_transfer.py

    r1718 r1721  
    88import pytest
    99
    10 import ftputil.compat
    1110import ftputil.file_transfer
    1211import ftputil.stat
     
    8786        """Return a `BytesIO` object containing `count` "random" bytes."""
    8887        ints = (random.randint(0, 255) for i in range(count))
    89         return ftputil.compat.bytes_from_ints(ints)
     88        return bytes(ints)
    9089
    9190    def test_chunkwise_transfer_without_remainder(self):
  • test/test_host.py

    r1719 r1721  
    1515
    1616import ftputil
    17 import ftputil.compat
    1817import ftputil.error
    1918import ftputil.tool
     
    3332    """
    3433    ordinal_list = [random.choice(pool) for i in range(size)]
    35     return ftputil.compat.bytes_from_ints(ordinal_list)
     34    return bytes(ordinal_list)
    3635
    3736
     
    526525        items = host.listdir("ä")
    527526        assert items == ["ö", "o"]
    528         #  Need explicit type check for Python 2
    529         for item in items:
    530             assert isinstance(item, ftputil.compat.unicode_type)
    531527        # Bytes
    532528        items = host.listdir(as_bytes("ä"))
    533529        assert items == [as_bytes("ö"), as_bytes("o")]
    534         #  Need explicit type check for Python 2
    535         for item in items:
    536             assert isinstance(item, ftputil.compat.bytes_type)
    537530
    538531    def test_chmod(self):
  • test/test_path.py

    r1719 r1721  
    99
    1010import ftputil
    11 import ftputil.compat
    1211import ftputil.error
    1312import ftputil.tool
     
    136135        Test whether the same string type as for the argument is returned.
    137136        """
    138         bytes_type = ftputil.compat.bytes_type
    139         unicode_type = ftputil.compat.unicode_type
    140137        method_names = ("abspath basename dirname join normcase normpath".
    141138                        split())
     
    177174        result = join(*parts)
    178175        assert result == "ä/ö/ü"
    179         #  Need explicit type check for Python 2
    180         assert isinstance(result, ftputil.compat.unicode_type)
    181176        # Only bytes
    182177        parts = [as_bytes(s) for s in "äöü"]
    183178        result = join(*parts)
    184179        assert result == as_bytes("ä/ö/ü")
    185         #  Need explicit type check for Python 2
    186         assert isinstance(result, ftputil.compat.bytes_type)
    187180        # Mixture of unicode and bytes
    188181        parts = ["ä", as_bytes("ö")]
  • test/test_real_ftp.py

    r1720 r1721  
    2020import pytest
    2121
    22 import ftputil.compat
    2322import ftputil.error
    2423import ftputil.file_transfer
     
    495494        # - dir
    496495        dir_stat = host.stat(dir_name)
    497         assert isinstance(dir_stat._st_name, ftputil.compat.unicode_type)
     496        assert isinstance(dir_stat._st_name, str)
    498497        assert host.listdir(dir_name) == ["_nonempty_"]
    499498        assert host.path.isdir(dir_name)
     
    502501        # - file
    503502        file_stat = host.stat(file_name)
    504         assert isinstance(file_stat._st_name, ftputil.compat.unicode_type)
     503        assert isinstance(file_stat._st_name, str)
    505504        assert not host.path.isdir(file_name)
    506505        assert host.path.isfile(file_name)
     
    916915        host = self.host
    917916        # `ftplib` under Python 3 only works correctly if the unicode
    918         # strings are decoded from latin1. Under Python 2, ftputil
    919         # is supposed to provide a compatible interface.
     917        # strings are decoded from latin1.
    920918        path = "äbc".encode("UTF-8").decode("latin1")
    921919        names = host.listdir(path)
  • test/test_stat.py

    r1718 r1721  
    99
    1010import ftputil
    11 import ftputil.compat
    1211import ftputil.error
    1312import ftputil.stat
     
    364363        """Test if the `repr` result looks like a named tuple."""
    365364        stat_result = self.stat._lstat("/home/sschwarzer/chemeng")
    366         # Only under Python 2, unicode strings have the `u` prefix.
    367365        # TODO: Make the value for `st_mtime` robust against DST "time
    368366        # zone" changes.
    369         if ftputil.compat.python_version == 2:
    370             expected_result = (
    371               b"StatResult(st_mode=17901, st_ino=None, st_dev=None, "
    372               b"st_nlink=2, st_uid=u'45854', st_gid=u'200', st_size=512, "
    373               b"st_atime=None, st_mtime=957391200.0, st_ctime=None)")
    374         else:
    375             expected_result = (
    376               "StatResult(st_mode=17901, st_ino=None, st_dev=None, "
    377               "st_nlink=2, st_uid='45854', st_gid='200', st_size=512, "
    378               "st_atime=None, st_mtime=957391200.0, st_ctime=None)")
     367        expected_result = (
     368          "StatResult(st_mode=17901, st_ino=None, st_dev=None, "
     369          "st_nlink=2, st_uid='45854', st_gid='200', st_size=512, "
     370          "st_atime=None, st_mtime=957391200.0, st_ctime=None)")
    379371        assert repr(stat_result) == expected_result
    380372
  • test/test_tool.py

    r1718 r1721  
    33# See the file LICENSE for licensing terms.
    44
    5 import ftputil.compat as compat
    65import ftputil.tool
    76
     
    1615        result = ftputil.tool.same_string_type_as(b"abc", "def")
    1716        assert result == b"def"
    18         assert isinstance(result, compat.bytes_type)
    1917
    2018    def test_to_unicode(self):
    2119        result = ftputil.tool.same_string_type_as("abc", b"def")
    2220        assert result == "def"
    23         assert isinstance(result, compat.unicode_type)
    2421
    2522    def test_both_bytes_type(self):
    2623        result = ftputil.tool.same_string_type_as(b"abc", b"def")
    2724        assert result == b"def"
    28         assert isinstance(result, compat.bytes_type)
    2925
    3026    def test_both_unicode_type(self):
    3127        result = ftputil.tool.same_string_type_as("abc", "def")
    3228        assert result == "def"
    33         assert isinstance(result, compat.unicode_type)
    3429
    3530
     
    3934        result = ftputil.tool.as_bytes(b"abc")
    4035        assert result == b"abc"
    41         assert isinstance(result, compat.bytes_type)
    4236        result = ftputil.tool.as_bytes("abc")
    4337        assert result == b"abc"
    44         assert isinstance(result, compat.bytes_type)
    45        
     38
    4639    def test_as_unicode(self):
    4740        result = ftputil.tool.as_unicode(b"abc")
    4841        assert result == "abc"
    49         assert isinstance(result, compat.unicode_type)
    5042        result = ftputil.tool.as_unicode("abc")
    5143        assert result == "abc"
    52         assert isinstance(result, compat.unicode_type)
    5344
    5445
     
    5849        string = "abc"
    5950        converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
    60         assert isinstance(converted_string, compat.bytes_type)
     51        assert converted_string == b"abc"
    6152
    6253    def test_dont_encode(self):
     
    6455        not_converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
    6556        assert string == not_converted_string
    66         assert isinstance(not_converted_string, compat.bytes_type)
Note: See TracChangeset for help on using the changeset viewer.