Changeset 1882:c6f5f6f0e027


Ignore:
Timestamp:
Jan 1, 2020, 6:55:42 PM (3 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
histedit_source:
f49f109e195e511e5ebb1080f3a2efb197b12637,2553a5b74263028c50873b4ed21ae06259227c7b
Message:
Use `join` from `posixpath`

The type checks in our custom `join` method were only needed to
enforce type consistency for Python 2. This consistency check is
already in Python 3's `posixpath.join` and it can also handle
`PathLike` objects.

Also remove the test code because we assume that Python's `join`
implementation is already tested properly.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/path.py

    r1865 r1882  
    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.
     
    4444        self.normcase = pp.normcase
    4545        self.normpath = pp.normpath
     46        self.join = pp.join
    4647
    4748    def abspath(self, path):
     
    8182        """
    8283        return self._host.stat(path).st_size
    83 
    84     @staticmethod
    85     def join(*paths):
    86         """
    87         Join the path components from `paths` and return the joined
    88         path.
    89 
    90         All of these paths must be either unicode strings or byte
    91         strings. If not, `join` raises a `TypeError`.
    92         """
    93         # These checks are implicitly done by Python 3, but not by
    94         # Python 2.
    95         all_paths_are_unicode = all((isinstance(path, str) for path in paths))
    96         all_paths_are_bytes = all((isinstance(path, bytes) for path in paths))
    97         if all_paths_are_unicode or all_paths_are_bytes:
    98             return posixpath.join(*paths)
    99         else:
    100             # Python 3 raises this exception for mixed strings
    101             # in `os.path.join`, so also use this exception.
    102             raise TypeError("can't mix unicode strings and bytes in path components")
    10384
    10485    # Check whether a path is a regular file/dir/link. For the first
  • test/test_path.py

    r1848 r1882  
    517517            assert host.path.islink(as_bytes("ü"))
    518518
    519     def test_join(self):
    520         """
    521         Test whether `FTPHost.path.join` accepts only arguments of
    522         the same string type and returns the same string type.
    523         """
    524         as_bytes = ftputil.tool.as_bytes
    525         script = [Call("__init__"), Call("pwd", result="/"), Call("close")]
    526         with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
    527             host.stat_cache.disable()
    528             join = host.path.join
    529             # Only unicode
    530             parts = ["ä", "ö", "ü"]
    531             result = join(*parts)
    532             assert result == "ä/ö/ü"
    533             # Only bytes
    534             parts = [as_bytes(s) for s in parts]
    535             result = join(*parts)
    536             assert result == as_bytes("ä/ö/ü")
    537             # Mixture of unicode and bytes
    538             parts = ["ä", as_bytes("ö")]
    539             with pytest.raises(TypeError):
    540                 join(*parts)
    541             parts = [as_bytes("ä"), as_bytes("ö"), "ü"]
    542             with pytest.raises(TypeError):
    543                 join(*parts)
    544 
    545519    def test_getmtime(self):
    546520        """
Note: See TracChangeset for help on using the changeset viewer.