~sschwarzer/ftputil

86d652d1aa23642e188f25c4e7224c134b92aa18 — Stefan Schwarzer 6 years ago aa30554
Raise `CommandNotImplementedError` if `rest` is used for text files

Using `rest` for text files is confusing, don't support it.

For example, if a UTF-8-encoded file starts with "äbcdef", a `rest`
argument 3 would start reading at byte 3, but at the same time at
character 2 ("c") since "ä" is encoded in two bytes. Line ending
conversions would also result in deviations between byte and character
offsets.
2 files changed, 22 insertions(+), 2 deletions(-)

M ftputil/file.py
M test/test_real_ftp.py
M ftputil/file.py => ftputil/file.py +6 -2
@@ 1,4 1,4 @@
# Copyright (C) 2003-2014, Stefan Schwarzer <sschwarzer@sschwarzer.net>
# Copyright (C) 2003-2015, Stefan Schwarzer <sschwarzer@sschwarzer.net>
# and ftputil contributors (see `doc/contributors.txt`)
# See the file LICENSE for licensing terms.



@@ 74,7 74,11 @@ class FTPFile(object):
        # Convenience variables
        is_binary_mode = "b" in mode
        is_read_mode = "r" in mode
        # Always use binary mode (see above).
        # `rest` is only allowed for binary mode.
        if (not is_binary_mode) and (rest is not None):
            raise ftputil.error.CommandNotImplementedError(
                    "`rest` argument can't be used for text files")
        # Always use binary mode (see comments above).
        transfer_type = "I"
        command = "TYPE {0}".format(transfer_type)
        with ftputil.error.ftplib_error_to_ftp_io_error:

M test/test_real_ftp.py => test/test_real_ftp.py +16 -0
@@ 802,6 802,22 @@ class TestRestArgument(RealFTPTest):
            data = fobj.read()
        self.assertEqual(data, b"abc123")

    def test_invalid_read_from_text_file(self):
        """
        If the `rest` argument is used for reading from a text file,
        a `CommandNotImplementedError` should be raised.
        """
        with self.assertRaises(ftputil.error.CommandNotImplementedError):
            self.host.open(self.TEST_FILE_NAME, "r", rest=3)

    def test_invalid_write_to_text_file(self):
        """
        If the `rest` argument is used for reading from a text file,
        a `CommandNotImplementedError` should be raised.
        """
        with self.assertRaises(ftputil.error.CommandNotImplementedError):
            self.host.open(self.TEST_FILE_NAME, "w", rest=3)


class TestOther(RealFTPTest):