Changeset 391 for python/trunk/Lib/test/test_largefile.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_largefile.py
r2 r391 1 1 """Test largefile support on system where this makes sense. 2 2 """ 3 4 from __future__ import print_function 3 5 4 6 import os … … 7 9 import unittest 8 10 from test.test_support import run_unittest, TESTFN, verbose, requires, \ 9 TestSkipped, unlink 11 unlink 12 import io # C implementation of io 13 import _pyio as pyio # Python implementation of io 10 14 11 15 try: … … 19 23 20 24 # create >2GB file (2GB = 2147483648 bytes) 21 size = 2500000000 L22 23 24 class TestCase(unittest.TestCase):25 size = 2500000000 26 27 28 class LargeFileTest(unittest.TestCase): 25 29 """Test that each file function works as expected for a large 26 30 (i.e. > 2GB, do we have to check > 4GB) files. … … 34 38 def test_seek(self): 35 39 if verbose: 36 print 'create large file via seek (may be sparse file) ...'37 with open(TESTFN, 'wb') as f:38 f.write( 'z')40 print('create large file via seek (may be sparse file) ...') 41 with self.open(TESTFN, 'wb') as f: 42 f.write(b'z') 39 43 f.seek(0) 40 44 f.seek(size) 41 f.write( 'a')45 f.write(b'a') 42 46 f.flush() 43 47 if verbose: 44 print 'check file size with os.fstat'48 print('check file size with os.fstat') 45 49 self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1) 46 50 47 51 def test_osstat(self): 48 52 if verbose: 49 print 'check file size with os.stat'53 print('check file size with os.stat') 50 54 self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1) 51 55 52 56 def test_seek_read(self): 53 57 if verbose: 54 print 'play around with seek() and read() with the built largefile'55 with open(TESTFN, 'rb') as f:56 self.assertEqual(f.tell(), 0) 57 self.assertEqual(f.read(1), 'z')58 print('play around with seek() and read() with the built largefile') 59 with self.open(TESTFN, 'rb') as f: 60 self.assertEqual(f.tell(), 0) 61 self.assertEqual(f.read(1), b'z') 58 62 self.assertEqual(f.tell(), 1) 59 63 f.seek(0) … … 78 82 self.assertEqual(f.tell(), size) 79 83 # the 'a' that was written at the end of file above 80 self.assertEqual(f.read(1), 'a')84 self.assertEqual(f.read(1), b'a') 81 85 f.seek(-size-1, 1) 82 self.assertEqual(f.read(1), 'z')86 self.assertEqual(f.read(1), b'z') 83 87 self.assertEqual(f.tell(), 1) 84 88 85 89 def test_lseek(self): 86 90 if verbose: 87 print 'play around with os.lseek() with the built largefile'88 with open(TESTFN, 'rb') as f:91 print('play around with os.lseek() with the built largefile') 92 with self.open(TESTFN, 'rb') as f: 89 93 self.assertEqual(os.lseek(f.fileno(), 0, 0), 0) 90 94 self.assertEqual(os.lseek(f.fileno(), 42, 0), 42) … … 96 100 self.assertEqual(os.lseek(f.fileno(), size, 0), size) 97 101 # the 'a' that was written at the end of file above 98 self.assertEqual(f.read(1), 'a')102 self.assertEqual(f.read(1), b'a') 99 103 100 104 def test_truncate(self): 101 105 if verbose: 102 print 'try truncate'103 with open(TESTFN, 'r+b') as f:106 print('try truncate') 107 with self.open(TESTFN, 'r+b') as f: 104 108 # this is already decided before start running the test suite 105 109 # but we do it anyway for extra protection 106 110 if not hasattr(f, 'truncate'): 107 raise TestSkipped, "open().truncate() not available on this system"111 raise unittest.SkipTest("open().truncate() not available on this system") 108 112 f.seek(0, 2) 109 113 # else we've lost track of the true size … … 121 125 f.seek(42) 122 126 f.truncate(newsize) 123 self.assertEqual(f.tell(), 42) # else pointer moved 127 if self.new_io: 128 self.assertEqual(f.tell(), 42) 124 129 f.seek(0, 2) 125 self.assertEqual(f.tell(), newsize) # else wasn't truncated 126 130 self.assertEqual(f.tell(), newsize) 127 131 # XXX truncate(larger than true size) is ill-defined 128 132 # across platform; cut it waaaaay back 129 133 f.seek(0) 130 134 f.truncate(1) 131 self.assertEqual(f.tell(), 0) # else pointer moved 135 if self.new_io: 136 self.assertEqual(f.tell(), 0) # else pointer moved 137 f.seek(0) 132 138 self.assertEqual(len(f.read()), 1) # else wasn't truncated 139 140 def test_seekable(self): 141 # Issue #5016; seekable() can return False when the current position 142 # is negative when truncated to an int. 143 if not self.new_io: 144 self.skipTest("builtin file doesn't have seekable()") 145 for pos in (2**31-1, 2**31, 2**31+1): 146 with self.open(TESTFN, 'rb') as f: 147 f.seek(pos) 148 self.assertTrue(f.seekable()) 133 149 134 150 … … 145 161 # (Skip this test on Windows, since we now always support 146 162 # large files.) 147 f = open(TESTFN, 'wb' )163 f = open(TESTFN, 'wb', buffering=0) 148 164 try: 149 165 # 2**31 == 2147483648 150 f.seek(2147483649 L)166 f.seek(2147483649) 151 167 # Seeking is not enough of a test: you must write and 152 168 # flush, too! 153 f.write( "x")169 f.write(b'x') 154 170 f.flush() 155 171 except (IOError, OverflowError): 156 172 f.close() 157 173 unlink(TESTFN) 158 raise TestSkipped, "filesystem does not have largefile support"174 raise unittest.SkipTest("filesystem does not have largefile support") 159 175 else: 160 176 f.close() 161 177 suite = unittest.TestSuite() 162 suite.addTest(TestCase('test_seek')) 163 suite.addTest(TestCase('test_osstat')) 164 suite.addTest(TestCase('test_seek_read')) 165 suite.addTest(TestCase('test_lseek')) 166 with open(TESTFN, 'w') as f: 167 if hasattr(f, 'truncate'): 168 suite.addTest(TestCase('test_truncate')) 169 unlink(TESTFN) 178 for _open, prefix in [(io.open, 'C'), (pyio.open, 'Py'), 179 (open, 'Builtin')]: 180 class TestCase(LargeFileTest): 181 pass 182 TestCase.open = staticmethod(_open) 183 TestCase.new_io = _open is not open 184 TestCase.__name__ = prefix + LargeFileTest.__name__ 185 suite.addTest(TestCase('test_seek')) 186 suite.addTest(TestCase('test_osstat')) 187 suite.addTest(TestCase('test_seek_read')) 188 suite.addTest(TestCase('test_lseek')) 189 with _open(TESTFN, 'wb') as f: 190 if hasattr(f, 'truncate'): 191 suite.addTest(TestCase('test_truncate')) 192 suite.addTest(TestCase('test_seekable')) 193 unlink(TESTFN) 170 194 try: 171 195 run_unittest(suite) … … 173 197 unlink(TESTFN) 174 198 175 176 199 if __name__ == '__main__': 177 200 test_main()
Note:
See TracChangeset
for help on using the changeset viewer.