Changeset 1879:831dacddb87c


Ignore:
Timestamp:
Jan 1, 2020, 6:12:40 PM (3 weeks ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Support `PathLike` objects in `as_bytes` and `as_str`
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/tool.py

    r1878 r1879  
    1 # Copyright (C) 2013-2019, Stefan Schwarzer
     1# Copyright (C) 2013-2020, Stefan Schwarzer
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    4848
    4949
    50 def as_bytes(string):
     50def as_bytes(path):
    5151    """
    52     Return the argument `string` converted to a `bytes` object if it's
     52    Return the argument `path` converted to a `bytes` object if it's
    5353    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.
    5461    """
    55     return same_string_type_as(b"", string)
     62    path = os.fspath(path)
     63    return same_string_type_as(b"", path)
    5664
    5765
    58 def as_str(string):
     66def as_str(path):
    5967    """
    60     Return the argument `string` converted to a unicode string if it's
     68    Return the argument `path` converted to a unicode string if it's
    6169    a `bytes` object. Otherwise just return the string.
     70
     71    Instead of passing a `bytes` or `str` object for `path`, you can
     72    pass a `PathLike` object that can be converted to a `bytes` or
     73    `str` object.
     74
     75    If the `path` can't be converted to a `bytes` or `str`, a `TypeError`
     76    is raised.
    6277    """
    63     return same_string_type_as("", string)
     78    path = os.fspath(path)
     79    return same_string_type_as("", path)
  • test/test_tool.py

    r1876 r1879  
    1 # Copyright (C) 2013-2018, Stefan Schwarzer
     1# Copyright (C) 2013-2020, Stefan Schwarzer
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    55import os
    66
     7import pytest
     8
    79import ftputil.tool
    810
    911
    10 class TypeSourcePath(os.PathLike):
     12class Path(os.PathLike):
    1113    """
    1214    Helper class for `TestSameStringTypeAs`
     
    2224class TestSameStringTypeAs:
    2325    @staticmethod
    24     def _test_string_and_pathlike_object(type_source, content_source, expected_result):
     26    def _test_string_and_pathlike_object(type_source, path, expected_result):
    2527        """
    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`.
     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`.
    3031
    31         `TypeSourcePath(type_source)` means that the type source
    32         string is wrapped in a `PathLike` object whose `__fspath__`
    33         method returns `type_source`.
     32        `Path(type_source)` means that the type source string is wrapped in a
     33        `PathLike` object whose `__fspath__` method returns `type_source`.
    3434
    35         `type_source` must be a unicode string or byte string.
     35        `type_source` must be a `bytes` or `str` object or a `PathLike` object.
    3636        """
    37         result = ftputil.tool.same_string_type_as(type_source, content_source)
     37        result = ftputil.tool.same_string_type_as(type_source, path)
    3838        assert result == expected_result
    39         #
    40         type_source = TypeSourcePath(type_source)
    41         result = ftputil.tool.same_string_type_as(type_source, content_source)
     39        result = ftputil.tool.same_string_type_as(Path(type_source), path)
    4240        assert result == expected_result
    4341
     
    5553
    5654
    57 class TestSimpleConversions:
    58     def test_as_bytes(self):
    59         result = ftputil.tool.as_bytes(b"abc")
    60         assert result == b"abc"
    61         result = ftputil.tool.as_bytes("abc")
    62         assert result == b"abc"
     55as_bytes = ftputil.tool.as_bytes
    6356
    64     def test_as_str(self):
    65         result = ftputil.tool.as_str(b"abc")
    66         assert result == "abc"
    67         result = ftputil.tool.as_str("abc")
    68         assert result == "abc"
     57
     58class 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
     76as_str = ftputil.tool.as_str
     77
     78
     79class TestAsStr:
     80    def test_from_bytes(self):
     81        assert as_str(b"abc") == "abc"
     82
     83    def test_from_str(self):
     84        assert as_str("abc") == "abc"
     85
     86    def test_from_bytes_path(self):
     87        assert as_str(Path(b"abc")) == "abc"
     88
     89    def test_from_str_path(self):
     90        assert as_str(Path("abc")) == "abc"
     91
     92    def test_type_error(self):
     93        with pytest.raises(TypeError):
     94            as_str(1)
Note: See TracChangeset for help on using the changeset viewer.