Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f791d3

Browse files
committed
Issue #6623: Remove deprecated Netrc class in the ftplib module.
Patch by Matt Chaput.
1 parent 8906f14 commit 8f791d3

5 files changed

Lines changed: 23 additions & 129 deletions

File tree

Doc/whatsnew/3.5.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,18 @@ Deprecated features
369369
Removed
370370
=======
371371

372+
API and Feature Removals
373+
------------------------
374+
375+
The following obsolete and previously deprecated APIs and features have been
376+
removed:
377+
372378
* The ``__version__`` attribute has been dropped from the email package. The
373379
email code hasn't been shipped separately from the stdlib for a long time,
374380
and the ``__version__`` string was not updated in the last few releases.
375381

382+
* The internal ``Netrc`` class in the :mod:`ftplib` module was deprecated in
383+
3.4, and has now been removed. (Contributed by Matt Chaput in :issue:`6623`.)
376384

377385
Porting to Python 3.5
378386
=====================

Lib/ftplib.py

Lines changed: 5 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import warnings
4343
from socket import _GLOBAL_DEFAULT_TIMEOUT
4444

45-
__all__ = ["FTP", "Netrc"]
45+
__all__ = ["FTP"]
4646

4747
# Magic number from <socket.h>
4848
MSG_OOB = 0x1 # Process data out of band
@@ -920,115 +920,6 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
920920
target.voidresp()
921921

922922

923-
class Netrc:
924-
"""Class to parse & provide access to 'netrc' format files.
925-
926-
See the netrc(4) man page for information on the file format.
927-
928-
WARNING: This class is obsolete -- use module netrc instead.
929-
930-
"""
931-
__defuser = None
932-
__defpasswd = None
933-
__defacct = None
934-
935-
def __init__(self, filename=None):
936-
warnings.warn("This class is deprecated, use the netrc module instead",
937-
DeprecationWarning, 2)
938-
if filename is None:
939-
if "HOME" in os.environ:
940-
filename = os.path.join(os.environ["HOME"],
941-
".netrc")
942-
else:
943-
raise OSError("specify file to load or set $HOME")
944-
self.__hosts = {}
945-
self.__macros = {}
946-
fp = open(filename, "r")
947-
in_macro = 0
948-
while 1:
949-
line = fp.readline()
950-
if not line:
951-
break
952-
if in_macro and line.strip():
953-
macro_lines.append(line)
954-
continue
955-
elif in_macro:
956-
self.__macros[macro_name] = tuple(macro_lines)
957-
in_macro = 0
958-
words = line.split()
959-
host = user = passwd = acct = None
960-
default = 0
961-
i = 0
962-
while i < len(words):
963-
w1 = words[i]
964-
if i+1 < len(words):
965-
w2 = words[i + 1]
966-
else:
967-
w2 = None
968-
if w1 == 'default':
969-
default = 1
970-
elif w1 == 'machine' and w2:
971-
host = w2.lower()
972-
i = i + 1
973-
elif w1 == 'login' and w2:
974-
user = w2
975-
i = i + 1
976-
elif w1 == 'password' and w2:
977-
passwd = w2
978-
i = i + 1
979-
elif w1 == 'account' and w2:
980-
acct = w2
981-
i = i + 1
982-
elif w1 == 'macdef' and w2:
983-
macro_name = w2
984-
macro_lines = []
985-
in_macro = 1
986-
break
987-
i = i + 1
988-
if default:
989-
self.__defuser = user or self.__defuser
990-
self.__defpasswd = passwd or self.__defpasswd
991-
self.__defacct = acct or self.__defacct
992-
if host:
993-
if host in self.__hosts:
994-
ouser, opasswd, oacct = \
995-
self.__hosts[host]
996-
user = user or ouser
997-
passwd = passwd or opasswd
998-
acct = acct or oacct
999-
self.__hosts[host] = user, passwd, acct
1000-
fp.close()
1001-
1002-
def get_hosts(self):
1003-
"""Return a list of hosts mentioned in the .netrc file."""
1004-
return self.__hosts.keys()
1005-
1006-
def get_account(self, host):
1007-
"""Returns login information for the named host.
1008-
1009-
The return value is a triple containing userid,
1010-
password, and the accounting field.
1011-
1012-
"""
1013-
host = host.lower()
1014-
user = passwd = acct = None
1015-
if host in self.__hosts:
1016-
user, passwd, acct = self.__hosts[host]
1017-
user = user or self.__defuser
1018-
passwd = passwd or self.__defpasswd
1019-
acct = acct or self.__defacct
1020-
return user, passwd, acct
1021-
1022-
def get_macros(self):
1023-
"""Return a list of all defined macro names."""
1024-
return self.__macros.keys()
1025-
1026-
def get_macro(self, macro):
1027-
"""Return a sequence of lines which define a named macro."""
1028-
return self.__macros[macro]
1029-
1030-
1031-
1032923
def test():
1033924
'''Test program.
1034925
Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
@@ -1042,6 +933,8 @@ def test():
1042933
print(test.__doc__)
1043934
sys.exit(0)
1044935

936+
import netrc
937+
1045938
debugging = 0
1046939
rcfile = None
1047940
while sys.argv[1] == '-d':
@@ -1056,14 +949,14 @@ def test():
1056949
ftp.set_debuglevel(debugging)
1057950
userid = passwd = acct = ''
1058951
try:
1059-
netrc = Netrc(rcfile)
952+
netrcobj = netrc.netrc(rcfile)
1060953
except OSError:
1061954
if rcfile is not None:
1062955
sys.stderr.write("Could not open account file"
1063956
" -- using anonymous login.")
1064957
else:
1065958
try:
1066-
userid, passwd, acct = netrc.get_account(host)
959+
userid, acct, passwd = netrcobj.authenticators(host)
1067960
except KeyError:
1068961
# no account for host
1069962
sys.stderr.write(

Lib/test/test_ftplib.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def push(self, what):
7676
super(DummyDTPHandler, self).push(what.encode('ascii'))
7777

7878
def handle_error(self):
79-
raise
79+
raise Exception
8080

8181

8282
class DummyFTPHandler(asynchat.async_chat):
@@ -121,7 +121,7 @@ def found_terminator(self):
121121
self.push('550 command "%s" not understood.' %cmd)
122122

123123
def handle_error(self):
124-
raise
124+
raise Exception
125125

126126
def push(self, data):
127127
asynchat.async_chat.push(self, data.encode('ascii') + b'\r\n')
@@ -299,7 +299,7 @@ def writable(self):
299299
return 0
300300

301301
def handle_error(self):
302-
raise
302+
raise Exception
303303

304304

305305
if ssl is not None:
@@ -397,7 +397,7 @@ def recv(self, buffer_size):
397397
raise
398398

399399
def handle_error(self):
400-
raise
400+
raise Exception
401401

402402
def close(self):
403403
if (isinstance(self.socket, ssl.SSLSocket) and
@@ -673,7 +673,7 @@ def test_entry(line, type=None, perm=None, unique=None, name=None):
673673
self.assertRaises(StopIteration, next, self.client.mlsd())
674674
set_data('')
675675
for x in self.client.mlsd():
676-
self.fail("unexpected data %s" % data)
676+
self.fail("unexpected data %s" % x)
677677

678678
def test_makeport(self):
679679
with self.client.makeport():
@@ -1053,19 +1053,8 @@ def testTimeoutDirectAccess(self):
10531053
ftp.close()
10541054

10551055

1056-
class TestNetrcDeprecation(TestCase):
1057-
1058-
def test_deprecation(self):
1059-
with support.temp_cwd(), support.EnvironmentVarGuard() as env:
1060-
env['HOME'] = os.getcwd()
1061-
open('.netrc', 'w').close()
1062-
with self.assertWarns(DeprecationWarning):
1063-
ftplib.Netrc()
1064-
1065-
1066-
10671056
def test_main():
1068-
tests = [TestFTPClass, TestTimeouts, TestNetrcDeprecation,
1057+
tests = [TestFTPClass, TestTimeouts,
10691058
TestIPv6Environment,
10701059
TestTLS_FTPClassMixin, TestTLS_FTPClass]
10711060

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ Godefroid Chapelle
233233
Brad Chapman
234234
Greg Chapman
235235
Mitch Chapman
236+
Matt Chaput
236237
Yogesh Chaudhari
237238
David Chaum
238239
Nicolas Chauvat

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ Core and Builtins
180180
Library
181181
-------
182182

183+
- Issue #6623: Remove deprecated Netrc class in the ftplib module. Patch by
184+
Matt Chaput.
185+
183186
- Issue #17381: Fixed handling of case-insensitive ranges in regular
184187
expressions.
185188

0 commit comments

Comments
 (0)