Changeset 1876:f0f5c7f9993d


Ignore:
Timestamp:
Jan 1, 2020, 1:23:36 AM (3 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
histedit_source:
637c3758d71658181f2698921d6160002736ff4e
Message:
Support `PathLike` objects in `same_string_type_as`

If a `PathLike` object is passed as `type_source` argument, the result
from its `__fspath__` method is used to determine the actual
`type_source`.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/tool.py

    r1868 r1876  
    66tool.py - helper code
    77"""
     8
     9import os
     10
    811
    912__all__ = ["same_string_type_as", "as_bytes", "as_str"]
     
    2528    from `content_source`.
    2629
    27     If the `type_source` and `content_source` don't have the same
    28     type, use `LOSSLESS_ENCODING` above to encode or decode, whatever
    29     operation is needed.
     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.
    3037    """
    31     if isinstance(type_source, bytes) and isinstance(content_source, str):
     38    actual_type_source = os.fspath(type_source)
     39    if isinstance(actual_type_source, bytes) and isinstance(content_source, str):
    3240        return content_source.encode(LOSSLESS_ENCODING)
    33     elif isinstance(type_source, str) and isinstance(content_source, bytes):
     41    elif isinstance(actual_type_source, str) and isinstance(content_source, bytes):
    3442        return content_source.decode(LOSSLESS_ENCODING)
    3543    else:
  • test/test_tool.py

    r1871 r1876  
    33# See the file LICENSE for licensing terms.
    44
     5import os
     6
    57import ftputil.tool
    68
    79
     10class TypeSourcePath(os.PathLike):
     11    """
     12    Helper class for `TestSameStringTypeAs`
     13    """
     14
     15    def __init__(self, type_source):
     16        self._type_source = type_source
     17
     18    def __fspath__(self):
     19        return self._type_source
     20
     21
    822class TestSameStringTypeAs:
     23    @staticmethod
     24    def _test_string_and_pathlike_object(type_source, content_source, expected_result):
     25        """
     26        Check if the results from
     27        `tool.same_string_type_as(type_source, content_source)` and
     28        `tool.same_string_type_as(TypeSourcePath(type_source), content_source)`
     29        both are the same as `expected_result`.
     30
     31        `TypeSourcePath(type_source)` means that the type source
     32        string is wrapped in a `PathLike` object whose `__fspath__`
     33        method returns `type_source`.
     34
     35        `type_source` must be a unicode string or byte string.
     36        """
     37        result = ftputil.tool.same_string_type_as(type_source, content_source)
     38        assert result == expected_result
     39        #
     40        type_source = TypeSourcePath(type_source)
     41        result = ftputil.tool.same_string_type_as(type_source, content_source)
     42        assert result == expected_result
     43
    944    def test_to_bytes(self):
    10         result = ftputil.tool.same_string_type_as(b"abc", "def")
    11         assert result == b"def"
     45        self._test_string_and_pathlike_object(b"abc", "def", expected_result=b"def")
    1246
    1347    def test_to_str(self):
    14         result = ftputil.tool.same_string_type_as("abc", b"def")
    15         assert result == "def"
     48        self._test_string_and_pathlike_object("abc", b"def", expected_result="def")
    1649
    1750    def test_both_bytes_type(self):
    18         result = ftputil.tool.same_string_type_as(b"abc", b"def")
    19         assert result == b"def"
     51        self._test_string_and_pathlike_object(b"abc", b"def", expected_result=b"def")
    2052
    2153    def test_both_str_type(self):
    22         result = ftputil.tool.same_string_type_as("abc", "def")
    23         assert result == "def"
     54        self._test_string_and_pathlike_object("abc", "def", expected_result="def")
    2455
    2556
Note: See TracChangeset for help on using the changeset viewer.