[Python-checkins] cpython: Issue #6623: Remove deprecated Netrc class in the ftplib module.

berker.peksag python-checkins at python.org
Sat Nov 1 09:46:01 CET 2014


https://hg.python.org/cpython/rev/ec196a99af8d
changeset:   93328:ec196a99af8d
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Sat Nov 01 10:45:57 2014 +0200
summary:
  Issue #6623: Remove deprecated Netrc class in the ftplib module.

Patch by Matt Chaput.

files:
  Doc/whatsnew/3.5.rst    |    8 +
  Lib/ftplib.py           |  117 +--------------------------
  Lib/test/test_ftplib.py |   23 +----
  Misc/ACKS               |    1 +
  Misc/NEWS               |    3 +
  5 files changed, 23 insertions(+), 129 deletions(-)


diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -369,10 +369,18 @@
 Removed
 =======
 
+API and Feature Removals
+------------------------
+
+The following obsolete and previously deprecated APIs and features have been
+removed:
+
 * The ``__version__`` attribute has been dropped from the email package.  The
   email code hasn't been shipped separately from the stdlib for a long time,
   and the ``__version__`` string was not updated in the last few releases.
 
+* The internal ``Netrc`` class in the :mod:`ftplib` module was deprecated in
+  3.4, and has now been removed. (Contributed by Matt Chaput in :issue:`6623`.)
 
 Porting to Python 3.5
 =====================
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -42,7 +42,7 @@
 import warnings
 from socket import _GLOBAL_DEFAULT_TIMEOUT
 
-__all__ = ["FTP", "Netrc"]
+__all__ = ["FTP"]
 
 # Magic number from <socket.h>
 MSG_OOB = 0x1                           # Process data out of band
@@ -920,115 +920,6 @@
     target.voidresp()
 
 
-class Netrc:
-    """Class to parse & provide access to 'netrc' format files.
-
-    See the netrc(4) man page for information on the file format.
-
-    WARNING: This class is obsolete -- use module netrc instead.
-
-    """
-    __defuser = None
-    __defpasswd = None
-    __defacct = None
-
-    def __init__(self, filename=None):
-        warnings.warn("This class is deprecated, use the netrc module instead",
-                      DeprecationWarning, 2)
-        if filename is None:
-            if "HOME" in os.environ:
-                filename = os.path.join(os.environ["HOME"],
-                                        ".netrc")
-            else:
-                raise OSError("specify file to load or set $HOME")
-        self.__hosts = {}
-        self.__macros = {}
-        fp = open(filename, "r")
-        in_macro = 0
-        while 1:
-            line = fp.readline()
-            if not line:
-                break
-            if in_macro and line.strip():
-                macro_lines.append(line)
-                continue
-            elif in_macro:
-                self.__macros[macro_name] = tuple(macro_lines)
-                in_macro = 0
-            words = line.split()
-            host = user = passwd = acct = None
-            default = 0
-            i = 0
-            while i < len(words):
-                w1 = words[i]
-                if i+1 < len(words):
-                    w2 = words[i + 1]
-                else:
-                    w2 = None
-                if w1 == 'default':
-                    default = 1
-                elif w1 == 'machine' and w2:
-                    host = w2.lower()
-                    i = i + 1
-                elif w1 == 'login' and w2:
-                    user = w2
-                    i = i + 1
-                elif w1 == 'password' and w2:
-                    passwd = w2
-                    i = i + 1
-                elif w1 == 'account' and w2:
-                    acct = w2
-                    i = i + 1
-                elif w1 == 'macdef' and w2:
-                    macro_name = w2
-                    macro_lines = []
-                    in_macro = 1
-                    break
-                i = i + 1
-            if default:
-                self.__defuser = user or self.__defuser
-                self.__defpasswd = passwd or self.__defpasswd
-                self.__defacct = acct or self.__defacct
-            if host:
-                if host in self.__hosts:
-                    ouser, opasswd, oacct = \
-                           self.__hosts[host]
-                    user = user or ouser
-                    passwd = passwd or opasswd
-                    acct = acct or oacct
-                self.__hosts[host] = user, passwd, acct
-        fp.close()
-
-    def get_hosts(self):
-        """Return a list of hosts mentioned in the .netrc file."""
-        return self.__hosts.keys()
-
-    def get_account(self, host):
-        """Returns login information for the named host.
-
-        The return value is a triple containing userid,
-        password, and the accounting field.
-
-        """
-        host = host.lower()
-        user = passwd = acct = None
-        if host in self.__hosts:
-            user, passwd, acct = self.__hosts[host]
-        user = user or self.__defuser
-        passwd = passwd or self.__defpasswd
-        acct = acct or self.__defacct
-        return user, passwd, acct
-
-    def get_macros(self):
-        """Return a list of all defined macro names."""
-        return self.__macros.keys()
-
-    def get_macro(self, macro):
-        """Return a sequence of lines which define a named macro."""
-        return self.__macros[macro]
-
-
-
 def test():
     '''Test program.
     Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
@@ -1042,6 +933,8 @@
         print(test.__doc__)
         sys.exit(0)
 
+    import netrc
+
     debugging = 0
     rcfile = None
     while sys.argv[1] == '-d':
@@ -1056,14 +949,14 @@
     ftp.set_debuglevel(debugging)
     userid = passwd = acct = ''
     try:
-        netrc = Netrc(rcfile)
+        netrcobj = netrc.netrc(rcfile)
     except OSError:
         if rcfile is not None:
             sys.stderr.write("Could not open account file"
                              " -- using anonymous login.")
     else:
         try:
-            userid, passwd, acct = netrc.get_account(host)
+            userid, acct, passwd = netrcobj.authenticators(host)
         except KeyError:
             # no account for host
             sys.stderr.write(
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -76,7 +76,7 @@
         super(DummyDTPHandler, self).push(what.encode('ascii'))
 
     def handle_error(self):
-        raise
+        raise Exception
 
 
 class DummyFTPHandler(asynchat.async_chat):
@@ -121,7 +121,7 @@
             self.push('550 command "%s" not understood.' %cmd)
 
     def handle_error(self):
-        raise
+        raise Exception
 
     def push(self, data):
         asynchat.async_chat.push(self, data.encode('ascii') + b'\r\n')
@@ -299,7 +299,7 @@
         return 0
 
     def handle_error(self):
-        raise
+        raise Exception
 
 
 if ssl is not None:
@@ -397,7 +397,7 @@
                 raise
 
         def handle_error(self):
-            raise
+            raise Exception
 
         def close(self):
             if (isinstance(self.socket, ssl.SSLSocket) and
@@ -673,7 +673,7 @@
         self.assertRaises(StopIteration, next, self.client.mlsd())
         set_data('')
         for x in self.client.mlsd():
-            self.fail("unexpected data %s" % data)
+            self.fail("unexpected data %s" % x)
 
     def test_makeport(self):
         with self.client.makeport():
@@ -1053,19 +1053,8 @@
         ftp.close()
 
 
-class TestNetrcDeprecation(TestCase):
-
-    def test_deprecation(self):
-        with support.temp_cwd(), support.EnvironmentVarGuard() as env:
-            env['HOME'] = os.getcwd()
-            open('.netrc', 'w').close()
-            with self.assertWarns(DeprecationWarning):
-                ftplib.Netrc()
-
-
-
 def test_main():
-    tests = [TestFTPClass, TestTimeouts, TestNetrcDeprecation,
+    tests = [TestFTPClass, TestTimeouts,
              TestIPv6Environment,
              TestTLS_FTPClassMixin, TestTLS_FTPClass]
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -233,6 +233,7 @@
 Brad Chapman
 Greg Chapman
 Mitch Chapman
+Matt Chaput
 Yogesh Chaudhari
 David Chaum
 Nicolas Chauvat
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -180,6 +180,9 @@
 Library
 -------
 
+- Issue #6623: Remove deprecated Netrc class in the ftplib module. Patch by
+  Matt Chaput.
+
 - Issue #17381: Fixed handling of case-insensitive ranges in regular
   expressions.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list