Changeset 1619:66ef713f4fe8


Ignore:
Timestamp:
Dec 23, 2015, 5:21:44 PM (4 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
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.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/file.py

    r1618 r1619  
    1 # Copyright (C) 2003-2014, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2003-2015, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    7575        is_binary_mode = "b" in mode
    7676        is_read_mode = "r" in mode
    77         # Always use binary mode (see above).
     77        # `rest` is only allowed for binary mode.
     78        if (not is_binary_mode) and (rest is not None):
     79            raise ftputil.error.CommandNotImplementedError(
     80                    "`rest` argument can't be used for text files")
     81        # Always use binary mode (see comments above).
    7882        transfer_type = "I"
    7983        command = "TYPE {0}".format(transfer_type)
  • test/test_real_ftp.py

    r1618 r1619  
    803803        self.assertEqual(data, b"abc123")
    804804
     805    def test_invalid_read_from_text_file(self):
     806        """
     807        If the `rest` argument is used for reading from a text file,
     808        a `CommandNotImplementedError` should be raised.
     809        """
     810        with self.assertRaises(ftputil.error.CommandNotImplementedError):
     811            self.host.open(self.TEST_FILE_NAME, "r", rest=3)
     812
     813    def test_invalid_write_to_text_file(self):
     814        """
     815        If the `rest` argument is used for reading from a text file,
     816        a `CommandNotImplementedError` should be raised.
     817        """
     818        with self.assertRaises(ftputil.error.CommandNotImplementedError):
     819            self.host.open(self.TEST_FILE_NAME, "w", rest=3)
     820
    805821
    806822class TestOther(RealFTPTest):
Note: See TracChangeset for help on using the changeset viewer.