22"""
33
44import os
5- import stat
65import sys
76import unittest
87import socket
98import shutil
109import threading
11- from test .support import requires , bigmemtest
10+ from test .support import requires , bigmemtest , requires_resource
1211from test .support import SHORT_TIMEOUT
1312from test .support import socket_helper
1413from test .support .os_helper import TESTFN , unlink
@@ -29,7 +28,7 @@ def setUp(self):
2928 mode = 'w+b'
3029
3130 with self .open (TESTFN , mode ) as f :
32- current_size = os .fstat (f .fileno ())[ stat . ST_SIZE ]
31+ current_size = os .fstat (f .fileno ()). st_size
3332 if current_size == size + 1 :
3433 return
3534
@@ -40,13 +39,13 @@ def setUp(self):
4039 f .seek (size )
4140 f .write (b'a' )
4241 f .flush ()
43- self .assertEqual (os .fstat (f .fileno ())[ stat . ST_SIZE ] , size + 1 )
42+ self .assertEqual (os .fstat (f .fileno ()). st_size , size + 1 )
4443
4544 @classmethod
4645 def tearDownClass (cls ):
4746 with cls .open (TESTFN , 'wb' ):
4847 pass
49- if not os .stat (TESTFN )[ stat . ST_SIZE ] == 0 :
48+ if not os .stat (TESTFN ). st_size == 0 :
5049 raise cls .failureException ('File was not truncated by opening '
5150 'with mode "wb"' )
5251 unlink (TESTFN2 )
@@ -67,7 +66,7 @@ def test_large_read(self, _size):
6766 self .assertEqual (f .tell (), size + 1 )
6867
6968 def test_osstat (self ):
70- self .assertEqual (os .stat (TESTFN )[ stat . ST_SIZE ] , size + 1 )
69+ self .assertEqual (os .stat (TESTFN ). st_size , size + 1 )
7170
7271 def test_seek_read (self ):
7372 with self .open (TESTFN , 'rb' ) as f :
@@ -142,6 +141,9 @@ def test_truncate(self):
142141 f .truncate (1 )
143142 self .assertEqual (f .tell (), 0 ) # else pointer moved
144143 f .seek (0 )
144+ # Verify readall on a truncated file is well behaved. read()
145+ # without a size can be unbounded, this should get just the byte
146+ # that remains.
145147 self .assertEqual (len (f .read ()), 1 ) # else wasn't truncated
146148
147149 def test_seekable (self ):
@@ -152,6 +154,22 @@ def test_seekable(self):
152154 f .seek (pos )
153155 self .assertTrue (f .seekable ())
154156
157+ @bigmemtest (size = size , memuse = 2 , dry_run = False )
158+ def test_seek_readall (self , _size ):
159+ # Seek which doesn't change position should readall successfully.
160+ with self .open (TESTFN , 'rb' ) as f :
161+ self .assertEqual (f .seek (0 , os .SEEK_CUR ), 0 )
162+ self .assertEqual (len (f .read ()), size + 1 )
163+
164+ # Seek which changes (or might change) position should readall
165+ # successfully.
166+ with self .open (TESTFN , 'rb' ) as f :
167+ self .assertEqual (f .seek (20 , os .SEEK_SET ), 20 )
168+ self .assertEqual (len (f .read ()), size - 19 )
169+
170+ with self .open (TESTFN , 'rb' ) as f :
171+ self .assertEqual (f .seek (- 3 , os .SEEK_END ), size - 2 )
172+ self .assertEqual (len (f .read ()), 3 )
155173
156174def skip_no_disk_space (path , required ):
157175 def decorator (fun ):
@@ -173,6 +191,7 @@ class TestCopyfile(LargeFileTest, unittest.TestCase):
173191 # Exact required disk space would be (size * 2), but let's give it a
174192 # bit more tolerance.
175193 @skip_no_disk_space (TESTFN , size * 2.5 )
194+ @requires_resource ('cpu' )
176195 def test_it (self ):
177196 # Internally shutil.copyfile() can use "fast copy" methods like
178197 # os.sendfile().
@@ -222,6 +241,7 @@ def run(sock):
222241 # Exact required disk space would be (size * 2), but let's give it a
223242 # bit more tolerance.
224243 @skip_no_disk_space (TESTFN , size * 2.5 )
244+ @requires_resource ('cpu' )
225245 def test_it (self ):
226246 port = socket_helper .find_unused_port ()
227247 with socket .create_server (("" , port )) as sock :
0 commit comments