From 0c48c435a65256e4eb4f7f5aacd601abe0bd375e Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Fri, 19 Jul 2024 11:58:45 -0700 Subject: [PATCH 1/3] GH-120754: Add more tests around seek + readall In the process of speeding up readall, A number of related tests (ex. large file tests in test_zipfile) found problems with the change I was making. This adds I/O tests to specifically test these cases to help ensure they don't regress and hopefully make debugging easier. This is part of the improvements from https://github.com/python/cpython/pull/121593#issuecomment-2222261986 --- Lib/test/test_largefile.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Lib/test/test_largefile.py b/Lib/test/test_largefile.py index 849b6cb3e50a19..cc828b40364946 100644 --- a/Lib/test/test_largefile.py +++ b/Lib/test/test_largefile.py @@ -141,6 +141,9 @@ def test_truncate(self): f.truncate(1) self.assertEqual(f.tell(), 0) # else pointer moved f.seek(0) + # Verify readall on a truncated file is well behaved. read() + # without a size can be unbounded, this should get just the byte + # that remains. self.assertEqual(len(f.read()), 1) # else wasn't truncated def test_seekable(self): @@ -151,6 +154,22 @@ def test_seekable(self): f.seek(pos) self.assertTrue(f.seekable()) + @bigmemtest(size=size, memuse=2, dry_run=False) + def test_seek_readall(self, _size): + # Seek which doesn't change position should readall successfully. + with self.open(TESTFN, 'rb') as f: + self.assertEqual(f.seek(0, os.SEEK_CUR), 0) + self.assertEqual(len(f.read()), size+1) + + # Seek which changes (or might change) position should readall + # successfully. + with self.open(TESTFN, 'rb') as f: + self.assertEqual(f.seek(20, os.SEEK_SET), 20) + self.assertEqual(len(f.read()), size-19) + + with self.open(TESTFN, 'rb') as f: + self.assertEqual(f.seek(-3, os.SEEK_END), size - 2) + self.assertEqual(len(f.read()), 3) def skip_no_disk_space(path, required): def decorator(fun): From a01afaeceaf2be4c4e8c24b5cdfbf281bae1bf6d Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Mon, 22 Jul 2024 01:46:37 -0700 Subject: [PATCH 2/3] Update Lib/test/test_largefile.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_largefile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_largefile.py b/Lib/test/test_largefile.py index cc828b40364946..09474a41b17120 100644 --- a/Lib/test/test_largefile.py +++ b/Lib/test/test_largefile.py @@ -159,7 +159,7 @@ def test_seek_readall(self, _size): # Seek which doesn't change position should readall successfully. with self.open(TESTFN, 'rb') as f: self.assertEqual(f.seek(0, os.SEEK_CUR), 0) - self.assertEqual(len(f.read()), size+1) + self.assertEqual(len(f.read()), size + 1) # Seek which changes (or might change) position should readall # successfully. From 7abd21ded49f7a4528320615316b11a37de69ea7 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Mon, 22 Jul 2024 01:46:42 -0700 Subject: [PATCH 3/3] Update Lib/test/test_largefile.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_largefile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_largefile.py b/Lib/test/test_largefile.py index 09474a41b17120..41f7b70e5cfe81 100644 --- a/Lib/test/test_largefile.py +++ b/Lib/test/test_largefile.py @@ -165,7 +165,7 @@ def test_seek_readall(self, _size): # successfully. with self.open(TESTFN, 'rb') as f: self.assertEqual(f.seek(20, os.SEEK_SET), 20) - self.assertEqual(len(f.read()), size-19) + self.assertEqual(len(f.read()), size - 19) with self.open(TESTFN, 'rb') as f: self.assertEqual(f.seek(-3, os.SEEK_END), size - 2)