Changeset 1720:a10aefe0f71f


Ignore:
Timestamp:
Dec 25, 2018, 11:31:04 PM (9 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Remove support for `M2Crypto`

M2Crypto was used to get FTP_TLS support in Python 2. Since we now
target Python 3.5+, users can and should use `ftplib.FTP_TLS` to
get FTP_TLS support.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ftputil/session.py

    r1713 r1720  
    1111
    1212import ftputil.tool
    13 
    14 try:
    15     import M2Crypto
    16     import M2Crypto.ftpslib
    17 except ImportError:
    18     M2Crypto = None
    1913
    2014
     
    5347
    5448    This function should work for the base classes `ftplib.FTP`,
    55     `ftplib.FTP_TLS` and `M2Crypto.ftpslib.FTP_TLS` with TLS security.
    56     Other base classes should work if they use the same API as
    57     `ftplib.FTP`.
     49    `ftplib.FTP_TLS`. Other base classes should work if they use the
     50    same API as `ftplib.FTP`.
    5851
    5952    Usage example:
    6053
    6154      my_session_factory = session_factory(
    62                              base_class=M2Crypto.ftpslib.FTP_TLS,
     55                             base_class=ftplib.FTP_TLS,
    6356                             use_passive_mode=True,
    6457                             encrypt_data_channel=True)
     
    7164
    7265        def __init__(self, host, user, password):
    73             # Don't use `super` in case `base_class` isn't a new-style
    74             # class (e. g. `ftplib.FTP` in Python 2).
    75             base_class.__init__(self)
     66            super().__init__()
    7667            self.connect(host, port)
    77             if self._use_m2crypto_ftpslib():
    78                 self.auth_tls()
    79                 self._fix_socket()
    8068            if debug_level is not None:
    8169                self.set_debuglevel(debug_level)
     
    8876                self.prot_p()
    8977
    90         def _use_m2crypto_ftpslib(self):
    91             """
    92             Return `True` if the base class to use is
    93             `M2Crypto.ftpslib.FTP_TLS`, else return `False`.
    94             """
    95             return (M2Crypto is not None and
    96                     issubclass(base_class, M2Crypto.ftpslib.FTP_TLS))
    97 
    98         def _fix_socket(self):
    99             """
    100             Change the socket object so that arguments to `sendall`
    101             are converted to byte strings before being used.
    102 
    103             See the ftputil ticket #78 for details:
    104             http://ftputil.sschwarzer.net/trac/ticket/78
    105             """
    106             original_sendall = self.sock.sendall
    107             # Bound method, therefore no `self` argument.
    108             def sendall(data):
    109                 data = ftputil.tool.as_bytes(data)
    110                 return original_sendall(data)
    111             self.sock.sendall = sendall
    112 
    11378    return Session
  • test/test_real_ftp.py

    r1719 r1720  
    964964        objects_after_test = len(gc.garbage)
    965965        assert not objects_after_test - objects_before_test
    966 
    967     @pytest.mark.skipif(
    968       ftputil.compat.python_version > 2,
    969       reason="test requires M2Crypto which only works on Python 2")
    970     def test_m2crypto_session(self):
    971         """
    972         Test if a session with `M2Crypto.ftpslib.FTP_TLS` is set up
    973         correctly and works with unicode input.
    974         """
    975         # See ticket #78.
    976         #
    977         # M2Crypto is only available for Python 2.
    978         import M2Crypto
    979         factory = ftputil.session.session_factory(
    980                     base_class=M2Crypto.ftpslib.FTP_TLS,
    981                     encrypt_data_channel=True)
    982         with ftputil.FTPHost(*self.login_data, session_factory=factory) as host:
    983             # Test if unicode argument works.
    984             files = host.listdir(".")
    985         assert "CONTENTS" in files
  • test/test_session.py

    r1718 r1720  
    2424    def connect(self, host, port):
    2525        self.add_call("connect", host, port)
    26 
    27     def _fix_socket(self):
    28         self.add_call("_fix_socket")
    2926
    3027    def set_debuglevel(self, value):
     
    120117                                 ("set_debuglevel", 1),
    121118                                 ("login", "user", "password")]
    122 
    123     def test_m2crypto_session(self):
    124         """Test call sequence for M2Crypto session."""
    125         factory = \
    126           ftputil.session.session_factory(base_class=EncryptedMockSession)
    127         # Return `True` to fake that this is a session deriving from
    128         # `M2Crypto.ftpslib.FTP_TLS`.
    129         factory._use_m2crypto_ftpslib = lambda self: True
    130         # Override `_fix_socket` here, not in `MockSession`. Since
    131         # the created session class _inherits_ from `MockSession`,
    132         # it would override the `_fix_socket` there.
    133         factory._fix_socket = lambda self: self.add_call("_fix_socket")
    134         session = factory("host", "user", "password")
    135         assert session.calls == [("connect", "host", 21),
    136                                  ("auth_tls",),
    137                                  ("_fix_socket",),
    138                                  ("login", "user", "password"),
    139                                  ("prot_p",)]
  • tox.ini

    r1695 r1720  
    55
    66[tox]
    7 #envlist = py27, py34, pypy
    8 envlist = py27, py35
     7#envlist = py34, pypy
     8envlist = py35, py36, py37
    99
    1010[testenv]
    11 commands = py.test test
     11commands = python -m pytest test
    1212deps =
    1313    pytest
    14 
    15 [testenv:py27]
    16 # setenv =
    17 #     # Used (hopefully) temporarily since M2Crypto build fails with
    18 #     # SWIG error
    19 #     PYTHONPATH=/usr/lib64/python2.7/site-packages
    20 deps =
    21     pytest
    22     M2Crypto
Note: See TracChangeset for help on using the changeset viewer.