55import stat
66import sys
77import unittest
8- from test .support import run_unittest , TESTFN , verbose , requires , \
9- unlink
8+ from test .support import TESTFN , requires , unlink
109import io # C implementation of io
1110import _pyio as pyio # Python implementation of io
1211
13- try :
14- import signal
15- # The default handler for SIGXFSZ is to abort the process.
16- # By ignoring it, system calls exceeding the file size resource
17- # limit will raise IOError instead of crashing the interpreter.
18- oldhandler = signal .signal (signal .SIGXFSZ , signal .SIG_IGN )
19- except (ImportError , AttributeError ):
20- pass
21-
22- # create >2GB file (2GB = 2147483648 bytes)
12+ # size of file to create (>2GB; 2GB == 2147483648 bytes)
2313size = 2500000000
2414
15+ class LargeFileTest :
16+ """Test that each file function works as expected for large
17+ (i.e. > 2GB) files.
18+ """
2519
26- class LargeFileTest (unittest .TestCase ):
27- """Test that each file function works as expected for a large
28- (i.e. > 2GB, do we have to check > 4GB) files.
20+ def setUp (self ):
21+ if os .path .exists (TESTFN ):
22+ mode = 'r+b'
23+ else :
24+ mode = 'w+b'
2925
30- NOTE: the order of execution of the test methods is important! test_seek
31- must run first to create the test file. File cleanup must also be handled
32- outside the test instances because of this.
26+ with self .open (TESTFN , mode ) as f :
27+ current_size = os .fstat (f .fileno ())[stat .ST_SIZE ]
28+ if current_size == size + 1 :
29+ return
3330
34- """
31+ if current_size == 0 :
32+ f .write (b'z' )
3533
36- def test_seek (self ):
37- if verbose :
38- print ('create large file via seek (may be sparse file) ...' )
39- with self .open (TESTFN , 'wb' ) as f :
40- f .write (b'z' )
4134 f .seek (0 )
4235 f .seek (size )
4336 f .write (b'a' )
4437 f .flush ()
45- if verbose :
46- print ('check file size with os.fstat' )
4738 self .assertEqual (os .fstat (f .fileno ())[stat .ST_SIZE ], size + 1 )
4839
40+ @classmethod
41+ def tearDownClass (cls ):
42+ with cls .open (TESTFN , 'wb' ):
43+ pass
44+ if not os .stat (TESTFN )[stat .ST_SIZE ] == 0 :
45+ raise cls .failureException ('File was not truncated by opening '
46+ 'with mode "wb"' )
47+
4948 def test_osstat (self ):
50- if verbose :
51- print ('check file size with os.stat' )
5249 self .assertEqual (os .stat (TESTFN )[stat .ST_SIZE ], size + 1 )
5350
5451 def test_seek_read (self ):
55- if verbose :
56- print ('play around with seek() and read() with the built largefile' )
5752 with self .open (TESTFN , 'rb' ) as f :
5853 self .assertEqual (f .tell (), 0 )
5954 self .assertEqual (f .read (1 ), b'z' )
@@ -85,8 +80,6 @@ def test_seek_read(self):
8580 self .assertEqual (f .tell (), 1 )
8681
8782 def test_lseek (self ):
88- if verbose :
89- print ('play around with os.lseek() with the built largefile' )
9083 with self .open (TESTFN , 'rb' ) as f :
9184 self .assertEqual (os .lseek (f .fileno (), 0 , 0 ), 0 )
9285 self .assertEqual (os .lseek (f .fileno (), 42 , 0 ), 42 )
@@ -100,13 +93,10 @@ def test_lseek(self):
10093 self .assertEqual (f .read (1 ), b'a' )
10194
10295 def test_truncate (self ):
103- if verbose :
104- print ('try truncate' )
10596 with self .open (TESTFN , 'r+b' ) as f :
106- # this is already decided before start running the test suite
107- # but we do it anyway for extra protection
10897 if not hasattr (f , 'truncate' ):
109- raise unittest .SkipTest ("open().truncate() not available on this system" )
98+ raise unittest .SkipTest ("open().truncate() not available "
99+ "on this system" )
110100 f .seek (0 , 2 )
111101 # else we've lost track of the true size
112102 self .assertEqual (f .tell (), size + 1 )
@@ -141,8 +131,16 @@ def test_seekable(self):
141131 f .seek (pos )
142132 self .assertTrue (f .seekable ())
143133
134+ def setUpModule ():
135+ try :
136+ import signal
137+ # The default handler for SIGXFSZ is to abort the process.
138+ # By ignoring it, system calls exceeding the file size resource
139+ # limit will raise OSError instead of crashing the interpreter.
140+ signal .signal (signal .SIGXFSZ , signal .SIG_IGN )
141+ except (ImportError , AttributeError ):
142+ pass
144143
145- def test_main ():
146144 # On Windows and Mac OSX this test comsumes large resources; It
147145 # takes a long time to build the >2GB file and takes >2GB of disk
148146 # space therefore the resource must be enabled to run this test.
@@ -158,35 +156,25 @@ def test_main():
158156 try :
159157 # 2**31 == 2147483648
160158 f .seek (2147483649 )
161- # Seeking is not enough of a test: you must write and
162- # flush, too!
159+ # Seeking is not enough of a test: you must write and flush, too!
163160 f .write (b'x' )
164161 f .flush ()
165162 except (IOError , OverflowError ):
163+ raise unittest .SkipTest ("filesystem does not have "
164+ "largefile support" )
165+ finally :
166166 f .close ()
167167 unlink (TESTFN )
168- raise unittest .SkipTest ("filesystem does not have largefile support" )
169- else :
170- f .close ()
171- suite = unittest .TestSuite ()
172- for _open , prefix in [(io .open , 'C' ), (pyio .open , 'Py' )]:
173- class TestCase (LargeFileTest ):
174- pass
175- TestCase .open = staticmethod (_open )
176- TestCase .__name__ = prefix + LargeFileTest .__name__
177- suite .addTest (TestCase ('test_seek' ))
178- suite .addTest (TestCase ('test_osstat' ))
179- suite .addTest (TestCase ('test_seek_read' ))
180- suite .addTest (TestCase ('test_lseek' ))
181- with _open (TESTFN , 'wb' ) as f :
182- if hasattr (f , 'truncate' ):
183- suite .addTest (TestCase ('test_truncate' ))
184- suite .addTest (TestCase ('test_seekable' ))
185- unlink (TESTFN )
186- try :
187- run_unittest (suite )
188- finally :
189- unlink (TESTFN )
168+
169+
170+ class CLargeFileTest (LargeFileTest , unittest .TestCase ):
171+ open = staticmethod (io .open )
172+
173+ class PyLargeFileTest (LargeFileTest , unittest .TestCase ):
174+ open = staticmethod (pyio .open )
175+
176+ def tearDownModule ():
177+ unlink (TESTFN )
190178
191179if __name__ == '__main__' :
192- test_main ()
180+ unittest . main ()
0 commit comments