Changeset 1900:102788710723


Ignore:
Timestamp:
Jan 2, 2020, 9:42:40 PM (3 weeks ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Remove `os.fspath` call from `same_string_type_as`

As with the `as_str` name, the implicit `os.fspath` call is
unexpected. Fortunately, the few call sites and unit tests were
easy to adapt.
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ftputil/path.py

    r1899 r1900  
    77"""
    88
     9import os
    910import posixpath
    1011import stat
     
    5455        if not self.isabs(path):
    5556            path = self.join(self._host.getcwd(), path)
    56         return ftputil.tool.same_string_type_as(original_path, self.normpath(path))
     57        return ftputil.tool.same_string_type_as(
     58            os.fspath(original_path), self.normpath(path)
     59        )
    5760
    5861    def exists(self, path):
  • ftputil/tool.py

    r1899 r1900  
    2323
    2424
    25 def same_string_type_as(type_source, path):
     25def same_string_type_as(type_source, string):
    2626    """
    27     Return a string of the same type as `type_source` with the content
    28     from `path`.
     27    Return a string of the same type as `type_source` with the content from
     28    `string`.
    2929
    30     `type_source` may be a `PathLike` object. In that case, the type
    31     source is determined as `type_source.__fspath__()`.
    32 
    33     If the `type_source` (possibly after the described transformation)
    34     and `content_source` don't have the same type, use
    35     `LOSSLESS_ENCODING` above to encode or decode, whatever operation
    36     is needed.
    37 
    38     If the `path` can't be converted to a `bytes` or `str`, a `TypeError`
    39     is raised.
     30    If the `type_source` and `string` don't have the same type, use
     31    `LOSSLESS_ENCODING` above to encode or decode, whatever operation is needed.
    4032    """
    41     actual_type_source = os.fspath(type_source)
    42     if isinstance(actual_type_source, bytes) and isinstance(path, str):
    43         return path.encode(LOSSLESS_ENCODING)
    44     elif isinstance(actual_type_source, str) and isinstance(path, bytes):
    45         return path.decode(LOSSLESS_ENCODING)
     33    if isinstance(type_source, bytes) and isinstance(string, str):
     34        return string.encode(LOSSLESS_ENCODING)
     35    elif isinstance(type_source, str) and isinstance(string, bytes):
     36        return string.decode(LOSSLESS_ENCODING)
    4637    else:
    47         return path
     38        return string
    4839
    4940
  • test/test_tool.py

    r1899 r1900  
    2222
    2323
     24same_string_type_as = ftputil.tool.same_string_type_as
     25
     26
    2427class TestSameStringTypeAs:
    2528    @staticmethod
    26     def _test_string_and_pathlike_object(type_source, path, expected_result):
     29    def _test_string(type_source, string, expected_result):
    2730        """
    28         Check if the results from `tool.same_string_type_as(type_source, path)`
    29         and `tool.same_string_type_as(Path(type_source), path)` both are the
    30         same as `expected_result`.
     31        Check if the result from `tool.same_string_type_as(type_source, string)`
     32        is the same as `expected_result`.
    3133
    32         `Path(type_source)` means that the type source string is wrapped in a
    33         `PathLike` object whose `__fspath__` method returns `type_source`.
    34 
    35         `type_source` must be a `bytes` or `str` object or a `PathLike` object.
     34        `type_source` must be a `bytes` or `str` object.
    3635        """
    3736        result = ftputil.tool.same_string_type_as(type_source, path)
    3837        assert result == expected_result
    39         result = ftputil.tool.same_string_type_as(Path(type_source), path)
    40         assert result == expected_result
    4138
    4239    def test_to_bytes(self):
    43         self._test_string_and_pathlike_object(b"abc", "def", expected_result=b"def")
     40        assert same_string_type_as(b"abc", "def") == b"def"
    4441
    4542    def test_to_str(self):
    46         self._test_string_and_pathlike_object("abc", b"def", expected_result="def")
     43        assert same_string_type_as("abc", b"def") == "def"
    4744
    4845    def test_both_bytes_type(self):
    49         self._test_string_and_pathlike_object(b"abc", b"def", expected_result=b"def")
     46        assert same_string_type_as(b"abc", b"def") == b"def"
    5047
    5148    def test_both_str_type(self):
    52         self._test_string_and_pathlike_object("abc", "def", expected_result="def")
     49        assert same_string_type_as("abc", "def") == "def"
    5350
    5451
Note: See TracChangeset for help on using the changeset viewer.