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

Skip to content

Commit 855fa14

Browse files
authored
Update ftplib and test_ftplib to 3.12 (#5196)
1 parent 4e7b3bc commit 855fa14

File tree

2 files changed

+15
-43
lines changed

2 files changed

+15
-43
lines changed

Lib/ftplib.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
434434
"""
435435
self.voidcmd('TYPE I')
436436
with self.transfercmd(cmd, rest) as conn:
437-
while 1:
438-
data = conn.recv(blocksize)
439-
if not data:
440-
break
437+
while data := conn.recv(blocksize):
441438
callback(data)
442439
# shutdown ssl layer
443440
if _SSLSocket is not None and isinstance(conn, _SSLSocket):
@@ -496,10 +493,7 @@ def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
496493
"""
497494
self.voidcmd('TYPE I')
498495
with self.transfercmd(cmd, rest) as conn:
499-
while 1:
500-
buf = fp.read(blocksize)
501-
if not buf:
502-
break
496+
while buf := fp.read(blocksize):
503497
conn.sendall(buf)
504498
if callback:
505499
callback(buf)
@@ -561,7 +555,7 @@ def dir(self, *args):
561555
LIST command. (This *should* only be used for a pathname.)'''
562556
cmd = 'LIST'
563557
func = None
564-
if args[-1:] and type(args[-1]) != type(''):
558+
if args[-1:] and not isinstance(args[-1], str):
565559
args, func = args[:-1], args[-1]
566560
for arg in args:
567561
if arg:
@@ -713,28 +707,12 @@ class FTP_TLS(FTP):
713707
'221 Goodbye.'
714708
>>>
715709
'''
716-
ssl_version = ssl.PROTOCOL_TLS_CLIENT
717710

718711
def __init__(self, host='', user='', passwd='', acct='',
719-
keyfile=None, certfile=None, context=None,
720-
timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None, *,
721-
encoding='utf-8'):
722-
if context is not None and keyfile is not None:
723-
raise ValueError("context and keyfile arguments are mutually "
724-
"exclusive")
725-
if context is not None and certfile is not None:
726-
raise ValueError("context and certfile arguments are mutually "
727-
"exclusive")
728-
if keyfile is not None or certfile is not None:
729-
import warnings
730-
warnings.warn("keyfile and certfile are deprecated, use a "
731-
"custom context instead", DeprecationWarning, 2)
732-
self.keyfile = keyfile
733-
self.certfile = certfile
712+
*, context=None, timeout=_GLOBAL_DEFAULT_TIMEOUT,
713+
source_address=None, encoding='utf-8'):
734714
if context is None:
735-
context = ssl._create_stdlib_context(self.ssl_version,
736-
certfile=certfile,
737-
keyfile=keyfile)
715+
context = ssl._create_stdlib_context()
738716
self.context = context
739717
self._prot_p = False
740718
super().__init__(host, user, passwd, acct,
@@ -749,7 +727,7 @@ def auth(self):
749727
'''Set up secure control connection by using TLS/SSL.'''
750728
if isinstance(self.sock, ssl.SSLSocket):
751729
raise ValueError("Already using TLS")
752-
if self.ssl_version >= ssl.PROTOCOL_TLS:
730+
if self.context.protocol >= ssl.PROTOCOL_TLS:
753731
resp = self.voidcmd('AUTH TLS')
754732
else:
755733
resp = self.voidcmd('AUTH SSL')

Lib/test/test_ftplib.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from test.support import threading_helper
2222
from test.support import socket_helper
2323
from test.support import warnings_helper
24+
from test.support import asynchat
25+
from test.support import asyncore
2426
from test.support.socket_helper import HOST, HOSTv6
2527

2628
import sys
@@ -992,11 +994,11 @@ def test_context(self):
992994
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
993995
ctx.check_hostname = False
994996
ctx.verify_mode = ssl.CERT_NONE
995-
self.assertRaises(ValueError, ftplib.FTP_TLS, keyfile=CERTFILE,
997+
self.assertRaises(TypeError, ftplib.FTP_TLS, keyfile=CERTFILE,
996998
context=ctx)
997-
self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE,
999+
self.assertRaises(TypeError, ftplib.FTP_TLS, certfile=CERTFILE,
9981000
context=ctx)
999-
self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE,
1001+
self.assertRaises(TypeError, ftplib.FTP_TLS, certfile=CERTFILE,
10001002
keyfile=CERTFILE, context=ctx)
10011003

10021004
self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT)
@@ -1160,18 +1162,10 @@ def test__all__(self):
11601162
support.check__all__(self, ftplib, not_exported=not_exported)
11611163

11621164

1163-
def test_main():
1164-
tests = [TestFTPClass, TestTimeouts,
1165-
TestIPv6Environment,
1166-
TestTLS_FTPClassMixin, TestTLS_FTPClass,
1167-
MiscTestCase]
1168-
1165+
def setUpModule():
11691166
thread_info = threading_helper.threading_setup()
1170-
try:
1171-
support.run_unittest(*tests)
1172-
finally:
1173-
threading_helper.threading_cleanup(*thread_info)
1167+
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
11741168

11751169

11761170
if __name__ == '__main__':
1177-
test_main()
1171+
unittest.main()

0 commit comments

Comments
 (0)