Changeset 1898:31ad8b3fcd2e


Ignore:
Timestamp:
Jan 2, 2020, 8:54:08 PM (3 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Remove `ftputil.tool.as_bytes`

It's no longer necesary in any of the modules.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ftputil/tool.py

    r1879 r1898  
    1010
    1111
    12 __all__ = ["same_string_type_as", "as_bytes", "as_str"]
     12__all__ = ["same_string_type_as", "as_str"]
    1313
    1414
     
    4848
    4949
    50 def as_bytes(path):
    51     """
    52     Return the argument `path` converted to a `bytes` object if it's
    53     a unicode string. Otherwise just return the `bytes` object.
    54 
    55     Instead of passing a `bytes` or `str` object for `path`, you can
    56     pass a `PathLike` object that can be converted to a `bytes` or
    57     `str` object.
    58 
    59     If the `path` can't be converted to a `bytes` or `str`, a `TypeError`
    60     is raised.
    61     """
    62     path = os.fspath(path)
    63     return same_string_type_as(b"", path)
    64 
    65 
    6650def as_str(path):
    6751    """
  • test/test_host.py

    r1848 r1898  
    1 # Copyright (C) 2002-2019, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2002-2020, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    4242    integer_list = [random.choice(pool) for i in range(size)]
    4343    return bytes(integer_list)
     44
     45
     46def as_bytes(string):
     47    return string.encode(ftputil.tool.LOSSLESS_ENCODING)
    4448
    4549
     
    695699        multisession_factory = scripted_session.factory(host_script, file_script)
    696700        with test_base.ftp_host_factory(multisession_factory) as host:
    697             host.upload("Makefile", ftputil.tool.as_bytes("target"))
     701            host.upload("Makefile", as_bytes("target"))
    698702
    699703    def test_download(self, tmp_path):
     
    720724        multisession_factory = scripted_session.factory(host_script, file_script)
    721725        with test_base.ftp_host_factory(multisession_factory) as host:
    722             host.download(ftputil.tool.as_bytes("source"), str(local_target))
     726            host.download(as_bytes("source"), str(local_target))
    723727
    724728    def test_rename(self):
     
    732736        ]
    733737        # It's possible to mix argument types, as for `os.rename`.
    734         path_as_unicode = "/ä"
    735         path_as_bytes = ftputil.tool.as_bytes(path_as_unicode)
    736         paths = [path_as_unicode, path_as_bytes]
     738        path_as_str = "/ä"
     739        path_as_bytes = as_bytes(path_as_str)
     740        paths = [path_as_str, path_as_bytes]
    737741        for source_path, target_path in itertools.product(paths, paths):
    738742            session_factory = scripted_session.factory(script)
     
    742746    def test_listdir(self):
    743747        """Test whether `listdir` accepts either unicode or bytes."""
    744         as_bytes = ftputil.tool.as_bytes
    745748        top_level_dir_line = test_base.dir_line(
    746749            mode_string="drwxr-xr-x", date_=datetime.date.today(), name="ä"
     
    796799        session_factory = scripted_session.factory(script)
    797800        with test_base.ftp_host_factory(session_factory) as host:
    798             host.chmod(ftputil.tool.as_bytes(path), 0o755)
     801            host.chmod(as_bytes(path), 0o755)
    799802
    800803    def _test_method_with_single_path_argument(self, method_name, path, script):
     
    808811        with test_base.ftp_host_factory(session_factory) as host:
    809812            method = getattr(host, method_name)
    810             method(ftputil.tool.as_bytes(path))
     813            method(as_bytes(path))
    811814
    812815    def test_chdir(self):
     
    10111014        session_factory = scripted_session.factory(script)
    10121015        with test_base.ftp_host_factory(session_factory) as host:
    1013             result = list(host.walk(ftputil.tool.as_bytes("/ä")))
     1016            result = list(host.walk(as_bytes("/ä")))
    10141017
    10151018
  • test/test_path.py

    r1884 r1898  
    1 # Copyright (C) 2003-2019, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2003-2020, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    1818
    1919Call = scripted_session.Call
     20
     21
     22def as_bytes(string):
     23    return string.encode(ftputil.tool.LOSSLESS_ENCODING)
    2024
    2125
     
    410414    def test_methods_that_take_a_string_and_return_a_bool(self):
    411415        """Test whether the methods accept byte and unicode strings."""
    412         as_bytes = ftputil.tool.as_bytes
    413416        path_converter = self.path_converter
    414417        script = [
     
    528531        paths.
    529532        """
    530         as_bytes = ftputil.tool.as_bytes
    531533        path_converter = self.path_converter
    532534        now = datetime.datetime.now()
     
    562564        Test whether `FTPHost.path.getsize` accepts byte and unicode paths.
    563565        """
    564         as_bytes = ftputil.tool.as_bytes
    565566        path_converter = self.path_converter
    566567        now = datetime.datetime.now()
     
    595596    def test_walk(self):
    596597        """Test whether `FTPHost.path.walk` accepts bytes and unicode paths."""
    597         as_bytes = ftputil.tool.as_bytes
    598598        path_converter = self.path_converter
    599599        now = datetime.datetime.now()
  • test/test_tool.py

    r1879 r1898  
    5353
    5454
    55 as_bytes = ftputil.tool.as_bytes
    56 
    57 
    58 class TestAsBytes:
    59     def test_from_bytes(self):
    60         assert as_bytes(b"abc") == b"abc"
    61 
    62     def test_from_str(self):
    63         assert as_bytes("abc") == b"abc"
    64 
    65     def test_from_bytes_path(self):
    66         assert as_bytes(Path(b"abc")) == b"abc"
    67 
    68     def test_from_str_path(self):
    69         assert as_bytes(Path("abc")) == b"abc"
    70 
    71     def test_type_error(self):
    72         with pytest.raises(TypeError):
    73             as_bytes(1)
    74 
    75 
    7655as_str = ftputil.tool.as_str
    7756
Note: See TracChangeset for help on using the changeset viewer.