Ignore:
Timestamp:
Apr 1, 2013, 5:57:03 PM (6 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Make `from __future__ import unicode_literals` work for all modules.

In some cases this required some changes which will also benefit
Python 3 compatibility.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftputil/file.py

    r1199 r1224  
    77"""
    88
     9from __future__ import unicode_literals
     10
     11import ftputil.compat
    912import ftputil.error
    1013
     
    5861        # Overwritten later in `_open`.
    5962        self._bin_mode = None
     63        #TODO: Allow encoding to be set in `_open`.
     64        self._encoding = "utf-8"
    6065        self._conn = None
    6166        self._read_mode = None
     
    175180        if not self._bin_mode:
    176181            data = _python_to_crlf_linesep(data)
     182            if ftputil.compat.python_version == 3:
     183                # For Python 3, always require that the data for text
     184                # mode writes is a unicode string.
     185                data = data.encode(self._encoding)
     186            else:
     187                # For Python 2, also accept byte strings for
     188                # compatibility with `open` semantics. Only encode
     189                # unicode strings.
     190                data = ftputil.tool.encode_if_unicode(data, self._encoding)
    177191        self._fo.write(data)
    178192
     
    186200        # given as argument `lines`.
    187201        for line in lines:
    188             self._fo.write(_python_to_crlf_linesep(line))
     202            self.write(line)
    189203
    190204    #
Note: See TracChangeset for help on using the changeset viewer.