From 80ea3a1db279fe4a3118de6ec40c06b615d0bd5e Mon Sep 17 00:00:00 2001 From: rudy-07 Date: Mon, 2 Nov 2026 02:22:55 +0530 Subject: [PATCH 1/2] 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. --- Lib/test/test_ftplib.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index 0467c3c782b61d..d5d6ff62a0857d 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -1242,11 +1242,14 @@ 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') From efc9fb7b46497b78fcdfabeff226b9bae4788c9c Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Sun, 1 Nov 2026 19:14:58 -0800 Subject: [PATCH 2/2] Fix the refleaks by not creating an instance of OSError --- Lib/test/test_ftplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index d5d6ff62a0857d..795527382b2abb 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -1253,7 +1253,7 @@ def test_cli_retrieve_file(self, mock_netrc, mock_ftp_cls): 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: