annotate Lib/test/test_largefile.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 228cfb6e2900
children 760a710eb6c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
1 """Test largefile support on system where this makes sense.
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
2 """
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
3
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
4 import os
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
5 import stat
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
6 import sys
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
7 import unittest
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
8 from test.test_support import run_unittest, TESTFN, verbose, requires, \
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
9 unlink
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
10
21346
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
11 try:
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
12 import signal
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
13 # The default handler for SIGXFSZ is to abort the process.
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
14 # By ignoring it, system calls exceeding the file size resource
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
15 # limit will raise IOError instead of crashing the interpreter.
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
16 oldhandler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
17 except (ImportError, AttributeError):
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
18 pass
8a8f57dcf9fe Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents: 19828
diff changeset
19
19827
39eeab689d06 Move the global variables 'size' and 'name' to the top -- these are
Guido van Rossum <guido@python.org>
parents: 19825
diff changeset
20 # create >2GB file (2GB = 2147483648 bytes)
39eeab689d06 Move the global variables 'size' and 'name' to the top -- these are
Guido van Rossum <guido@python.org>
parents: 19825
diff changeset
21 size = 2500000000L
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
22
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
23
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
24 class TestCase(unittest.TestCase):
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
25 """Test that each file function works as expected for a large
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
26 (i.e. > 2GB, do we have to check > 4GB) files.
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
27
45628
c6d9a34f024f Add a note in the main test class' docstring that the order of execution of the
Brett Cannon <bcannon@gmail.com>
parents: 45626
diff changeset
28 NOTE: the order of execution of the test methods is important! test_seek
c6d9a34f024f Add a note in the main test class' docstring that the order of execution of the
Brett Cannon <bcannon@gmail.com>
parents: 45626
diff changeset
29 must run first to create the test file. File cleanup must also be handled
c6d9a34f024f Add a note in the main test class' docstring that the order of execution of the
Brett Cannon <bcannon@gmail.com>
parents: 45626
diff changeset
30 outside the test instances because of this.
c6d9a34f024f Add a note in the main test class' docstring that the order of execution of the
Brett Cannon <bcannon@gmail.com>
parents: 45626
diff changeset
31
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
32 """
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
33
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
34 def test_seek(self):
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
35 if verbose:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
36 print 'create large file via seek (may be sparse file) ...'
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
37 with open(TESTFN, 'wb') as f:
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
38 f.write('z')
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
39 f.seek(0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
40 f.seek(size)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
41 f.write('a')
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
42 f.flush()
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
43 if verbose:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
44 print 'check file size with os.fstat'
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
45 self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
46
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
47 def test_osstat(self):
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
48 if verbose:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
49 print 'check file size with os.stat'
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
50 self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1)
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
51
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
52 def test_seek_read(self):
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
53 if verbose:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
54 print 'play around with seek() and read() with the built largefile'
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
55 with open(TESTFN, 'rb') as f:
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
56 self.assertEqual(f.tell(), 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
57 self.assertEqual(f.read(1), 'z')
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
58 self.assertEqual(f.tell(), 1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
59 f.seek(0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
60 self.assertEqual(f.tell(), 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
61 f.seek(0, 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
62 self.assertEqual(f.tell(), 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
63 f.seek(42)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
64 self.assertEqual(f.tell(), 42)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
65 f.seek(42, 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
66 self.assertEqual(f.tell(), 42)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
67 f.seek(42, 1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
68 self.assertEqual(f.tell(), 84)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
69 f.seek(0, 1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
70 self.assertEqual(f.tell(), 84)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
71 f.seek(0, 2) # seek from the end
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
72 self.assertEqual(f.tell(), size + 1 + 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
73 f.seek(-10, 2)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
74 self.assertEqual(f.tell(), size + 1 - 10)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
75 f.seek(-size-1, 2)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
76 self.assertEqual(f.tell(), 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
77 f.seek(size)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
78 self.assertEqual(f.tell(), size)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
79 # the 'a' that was written at the end of file above
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
80 self.assertEqual(f.read(1), 'a')
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
81 f.seek(-size-1, 1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
82 self.assertEqual(f.read(1), 'z')
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
83 self.assertEqual(f.tell(), 1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
84
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
85 def test_lseek(self):
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
86 if verbose:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
87 print 'play around with os.lseek() with the built largefile'
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
88 with open(TESTFN, 'rb') as f:
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
89 self.assertEqual(os.lseek(f.fileno(), 0, 0), 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
90 self.assertEqual(os.lseek(f.fileno(), 42, 0), 42)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
91 self.assertEqual(os.lseek(f.fileno(), 42, 1), 84)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
92 self.assertEqual(os.lseek(f.fileno(), 0, 1), 84)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
93 self.assertEqual(os.lseek(f.fileno(), 0, 2), size+1+0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
94 self.assertEqual(os.lseek(f.fileno(), -10, 2), size+1-10)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
95 self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
96 self.assertEqual(os.lseek(f.fileno(), size, 0), size)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
97 # the 'a' that was written at the end of file above
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
98 self.assertEqual(f.read(1), 'a')
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
99
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
100 def test_truncate(self):
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
101 if verbose:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
102 print 'try truncate'
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
103 with open(TESTFN, 'r+b') as f:
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
104 # this is already decided before start running the test suite
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
105 # but we do it anyway for extra protection
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
106 if not hasattr(f, 'truncate'):
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
107 raise unittest.SkipTest, "open().truncate() not available on this system"
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
108 f.seek(0, 2)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
109 # else we've lost track of the true size
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
110 self.assertEqual(f.tell(), size+1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
111 # Cut it back via seek + truncate with no argument.
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
112 newsize = size - 10
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
113 f.seek(newsize)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
114 f.truncate()
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
115 self.assertEqual(f.tell(), newsize) # else pointer moved
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
116 f.seek(0, 2)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
117 self.assertEqual(f.tell(), newsize) # else wasn't truncated
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
118 # Ensure that truncate(smaller than true size) shrinks
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
119 # the file.
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
120 newsize -= 1
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
121 f.seek(42)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
122 f.truncate(newsize)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
123 self.assertEqual(f.tell(), 42) # else pointer moved
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
124 f.seek(0, 2)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
125 self.assertEqual(f.tell(), newsize) # else wasn't truncated
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
126
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
127 # XXX truncate(larger than true size) is ill-defined
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
128 # across platform; cut it waaaaay back
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
129 f.seek(0)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
130 f.truncate(1)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
131 self.assertEqual(f.tell(), 0) # else pointer moved
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
132 self.assertEqual(len(f.read()), 1) # else wasn't truncated
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
133
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
134
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
135 def test_main():
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
136 # On Windows and Mac OSX this test comsumes large resources; It
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
137 # takes a long time to build the >2GB file and takes >2GB of disk
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
138 # space therefore the resource must be enabled to run this test.
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
139 # If not, nothing after this line stanza will be executed.
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
140 if sys.platform[:3] == 'win' or sys.platform == 'darwin':
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
141 requires('largefile',
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
142 'test requires %s bytes and a long time to run' % str(size))
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
143 else:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
144 # Only run if the current filesystem supports large files.
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
145 # (Skip this test on Windows, since we now always support
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
146 # large files.)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
147 f = open(TESTFN, 'wb')
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
148 try:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
149 # 2**31 == 2147483648
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
150 f.seek(2147483649L)
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
151 # Seeking is not enough of a test: you must write and
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
152 # flush, too!
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
153 f.write("x")
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
154 f.flush()
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
155 except (IOError, OverflowError):
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
156 f.close()
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
157 unlink(TESTFN)
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
158 raise unittest.SkipTest, "filesystem does not have largefile support"
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
159 else:
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
160 f.close()
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
161 suite = unittest.TestSuite()
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
162 suite.addTest(TestCase('test_seek'))
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
163 suite.addTest(TestCase('test_osstat'))
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
164 suite.addTest(TestCase('test_seek_read'))
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
165 suite.addTest(TestCase('test_lseek'))
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
166 with open(TESTFN, 'w') as f:
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
167 if hasattr(f, 'truncate'):
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
168 suite.addTest(TestCase('test_truncate'))
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
169 unlink(TESTFN)
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
170 try:
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
171 run_unittest(suite)
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
172 finally:
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
173 unlink(TESTFN)
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff changeset
174
22241
7584b6f1afde Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents: 22214
diff changeset
175
45216
7dc9dee099e8 Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents: 44904
diff changeset
176 if __name__ == '__main__':
45626
b932f035fb3e Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents: 45216
diff changeset
177 test_main()