https://github.com/python/cpython/commit/3d81c051234bf0c3f7bd37bb2434a473ff383cdd
commit: 3d81c051234bf0c3f7bd37bb2434a473ff383cdd
branch: main
author: Rudransh Shekhar <[email protected]>
committer: orsenthil <[email protected]>
date: 2026-11-02T03:43:15Z
summary:

gh-131178: Fix test_cli_retrieve_file when stdout is StringIO in test_ftplib 
(#161135)

* gh-131178: Mock sys.stdout in test_ftplib CLI retrieve test

Mock sys.stdout in CommandLineTest.test_cli_retrieve_file so that
accessing sys.stdout.buffer does not fail when regrtest or other test
runners wrap sys.stdout in an io.StringIO object.

* Fix the refleaks by not  creating an instance of OSError

---------

Co-authored-by: Senthil Kumaran <[email protected]>

files:
M Lib/test/test_ftplib.py

diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
index 0467c3c782b61d..795527382b2abb 100644
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -1242,15 +1242,18 @@ def test_cli_commands(self, mock_netrc, mock_ftp_cls):
     @mock.patch('netrc.netrc', side_effect=OSError)
     def test_cli_retrieve_file(self, mock_netrc, mock_ftp_cls):
         mock_ftp = mock_ftp_cls.return_value
-        with mock.patch('sys.argv', ['ftplib', 'example.com', 'testfile.txt']):
+        with mock.patch('sys.argv', ['ftplib', 'example.com', 
'testfile.txt']), \
+             mock.patch('sys.stdout') as mock_stdout:
             ftplib.test()
-        mock_ftp.retrbinary.assert_called_once()
-        args, kwargs = mock_ftp.retrbinary.call_args
-        self.assertEqual(args[0], 'RETR testfile.txt')
+        mock_ftp.retrbinary.assert_called_once_with(
+            'RETR testfile.txt', mock_stdout.buffer.write, 1024
+        )
+        mock_stdout.buffer.flush.assert_called_once()
+        mock_stdout.flush.assert_called_once()
         mock_ftp.quit.assert_called_once()
 
     @mock.patch('ftplib.FTP')
-    @mock.patch('netrc.netrc', side_effect=OSError('file not found'))
+    @mock.patch('netrc.netrc', side_effect=OSError)
     def test_cli_custom_rcfile_error(self, mock_netrc, mock_ftp_cls):
         with mock.patch('sys.argv', ['ftplib', '-rnonexistent.netrc', 
'example.com']), \
              support.captured_stderr() as stderr:

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to