Changeset 1483:0079a3a8be44


Ignore:
Timestamp:
Apr 19, 2014, 11:16:52 PM (5 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Avoid `UnicodeDecodeError` for non-ASCII server messages.

See ticket #77.

The test simulates the server error by raising an `ftplib.error_perm`
directly.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/error.py

    r1481 r1483  
    132132            # If `exc_value.args[0]` is present, assume it's a byte or
    133133            # unicode string.
    134             if exc_value.args and exc_value.args[0].startswith("502"):
     134            if (
     135              exc_value.args and
     136              ftputil.tool.as_unicode(exc_value.args[0]).startswith("502")
     137            ):
    135138                raise CommandNotImplementedError(*exc_value.args)
    136139            else:
  • test/test_error.py

    r1481 r1483  
    4848            self.fail()
    4949
     50    def test_ftplib_error_to_ftp_os_error_non_ascii_server_message(self):
     51        """
     52        Test that we don't get a `UnicodeDecodeError` if the server
     53        sends a message containing non-ASCII characters.
     54        """
     55        # See ticket #77.
     56        message = \
     57          ftputil.tool.as_bytes("Não é possível criar um arquivo já existente.")
     58        try:
     59            with ftputil.error.ftplib_error_to_ftp_os_error:
     60                raise ftplib.error_perm(message)
     61        # We expect a `PermanentError`.
     62        except ftputil.error.PermanentError:
     63            pass
     64        except UnicodeDecodeError:
     65            self.fail()
     66
    5067    def test_ftplib_error_to_ftp_io_error(self):
    5168        """
Note: See TracChangeset for help on using the changeset viewer.