Changeset 2029:6140fb27fe6e


Ignore:
Timestamp:
Jan 27, 2021, 4:29:33 PM (10 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
amend_source:
8cd7e529df03d962bdf83e62e86d675815b4ed8a
Message:
Use base class default if `encoding` isn't given

Make the `encoding` argument of `session_factory` default to `None`.
If `None`, use the default encoding of the base class. This follows
the same logic as for the default handling of the `use_passive_mode`
argument.

Note that the default encoding of `ftplib.FTP` changed from "latin-1"
in Python <= 3.8 to "utf-8" in Python >= 3.9. Therefore, if you want
the same encoding regardless of the Python version, pass your own base
class (compatible with `ftplib.FTP`) which uses a fixed encoding or
pass the `encoding` explicitly to `session_factory`.

ticket: 143
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/session.py

    r2028 r2029  
    2525    *,
    2626    encrypt_data_channel=True,
    27     encoding=ftputil.tool.DEFAULT_ENCODING,
     27    encoding=None,
    2828    debug_level=None,
    2929):
     
    4848    encoding: Encoding (str) to use for directory and file paths. Unicode paths
    4949    will be encoded with this encoding. Bytes paths are assumed to be in this
    50     encoding. The default is "latin-1". If you use Python 3.9 and want the
    51     default encoding for `ftplib.FTP` sessions, you need to pass "UTF-8"
    52     explicitly.
     50    encoding. The default (equivalent to passing `None`) is to use the default
     51    encoding of the `base_class` argument. Note that this has changed from
     52    Python 3.8 to 3.9. In Python 3.8 and lower, the default encoding is
     53    "latin-1"; in Python 3.9, the default encoding is "utf-8". Therefore, if
     54    you want an encoding that's independent of the Python version, pass an
     55    explicit `encoding`.
    5356
    5457    debug_level: Debug level (integer) to be set on a session instance. The
     
    8588        # set a custom encoding (per instance).
    8689        def __init__(self, host, user, password):
    87             if (sys.version_info.major, sys.version_info.minor) <= (3, 8):
     90            if (encoding is not None) and (
     91                sys.version_info.major,
     92                sys.version_info.minor,
     93            ) >= (3, 9):
     94                super().__init__(encoding=encoding)
     95            else:
    8896                super().__init__()
    89             else:
    90                 super().__init__(encoding=encoding)
    9197            self.connect(host, port)
    9298            if debug_level is not None:
     
    100106                self.prot_p()
    101107
    102     if (sys.version_info.major, sys.version_info.minor) <= (3, 8):
     108    # fmt: off
     109    if (encoding is not None) and (
     110        sys.version_info.major,
     111        sys.version_info.minor
     112    ) <= (3, 8):
    103113        Session.encoding = encoding
     114    # fmt: on
    104115    return Session
  • test/test_session.py

    r2028 r2029  
    77"""
    88
     9import sys
     10
    911import ftputil.session
    10 from ftputil.tool import DEFAULT_ENCODING
     12import ftputil.tool
    1113
    1214
     
    1517    Mock session base class to determine if all expected calls have happened.
    1618    """
     19
     20    encoding = ftputil.tool.DEFAULT_ENCODING
    1721
    1822    def __init__(self):
     
    125129        """
    126130        Test setting the default encoding and a custom encoding.
    127 
    128         (The default encoding, as in earlier ftputil versions, is "latin-1".)
    129131        """
    130132        # Default encoding
     
    137139            ("login", "user", "password"),
    138140        ]
    139         assert session.encoding == DEFAULT_ENCODING
     141        if (sys.version_info.major, sys.version_info.minor) <= (3, 8):
     142            assert session.encoding == "latin-1"
     143        else:
     144            assert session.encoding == "utf-8"
    140145        # Custom encoding
    141146        factory = ftputil.session.session_factory(
Note: See TracChangeset for help on using the changeset viewer.