Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_largefile.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 77130f2fe944 |
| children |
| 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 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
4 from __future__ import print_function |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
5 |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
6 import os |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
7 import stat |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
8 import sys |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
9 import unittest |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
10 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
|
11 unlink |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
12 import io # C implementation of io |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
13 import _pyio as pyio # Python implementation of io |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff
changeset
|
14 |
|
21346
8a8f57dcf9fe
Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents:
19828
diff
changeset
|
15 try: |
|
8a8f57dcf9fe
Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents:
19828
diff
changeset
|
16 import signal |
|
8a8f57dcf9fe
Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents:
19828
diff
changeset
|
17 # 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
|
18 # 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
|
19 # 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
|
20 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
|
21 except (ImportError, AttributeError): |
|
8a8f57dcf9fe
Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents:
19828
diff
changeset
|
22 pass |
|
8a8f57dcf9fe
Ignore SIGXFSZ. Fixes #490453.
Martin v. Löwis <martin@v.loewis.de>
parents:
19828
diff
changeset
|
23 |
|
19827
39eeab689d06
Move the global variables 'size' and 'name' to the top -- these are
Guido van Rossum <guido@python.org>
parents:
19825
diff
changeset
|
24 # create >2GB file (2GB = 2147483648 bytes) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
25 size = 2500000000 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff
changeset
|
26 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff
changeset
|
27 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
28 class LargeFileTest(unittest.TestCase): |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
29 """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
|
30 (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
|
31 |
|
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
|
32 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
|
33 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
|
34 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
|
35 |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
36 """ |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
37 |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
38 def test_seek(self): |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
39 if verbose: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
40 print('create large file via seek (may be sparse file) ...') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
41 with self.open(TESTFN, 'wb') as f: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
42 f.write(b'z') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
43 f.seek(0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
44 f.seek(size) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
45 f.write(b'a') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
46 f.flush() |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
47 if verbose: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
48 print('check file size with os.fstat') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
49 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
|
50 |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
51 def test_osstat(self): |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
52 if verbose: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
53 print('check file size with os.stat') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
54 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
|
55 |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
56 def test_seek_read(self): |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
57 if verbose: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
58 print('play around with seek() and read() with the built largefile') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
59 with self.open(TESTFN, 'rb') as f: |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
60 self.assertEqual(f.tell(), 0) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
61 self.assertEqual(f.read(1), b'z') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
62 self.assertEqual(f.tell(), 1) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
63 f.seek(0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
64 self.assertEqual(f.tell(), 0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
65 f.seek(0, 0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
66 self.assertEqual(f.tell(), 0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
67 f.seek(42) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
68 self.assertEqual(f.tell(), 42) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
69 f.seek(42, 0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
70 self.assertEqual(f.tell(), 42) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
71 f.seek(42, 1) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
72 self.assertEqual(f.tell(), 84) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
73 f.seek(0, 1) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
74 self.assertEqual(f.tell(), 84) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
75 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
|
76 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
|
77 f.seek(-10, 2) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
78 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
|
79 f.seek(-size-1, 2) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
80 self.assertEqual(f.tell(), 0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
81 f.seek(size) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
82 self.assertEqual(f.tell(), size) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
83 # the 'a' that was written at the end of file above |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
84 self.assertEqual(f.read(1), b'a') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
85 f.seek(-size-1, 1) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
86 self.assertEqual(f.read(1), b'z') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
87 self.assertEqual(f.tell(), 1) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
88 |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
89 def test_lseek(self): |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
90 if verbose: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
91 print('play around with os.lseek() with the built largefile') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
92 with self.open(TESTFN, 'rb') as f: |
|
45216
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, 0), 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(), 42, 0), 42) |
|
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(), 42, 1), 84) |
|
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(), 0, 1), 84) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 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
|
101 # the 'a' that was written at the end of file above |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
102 self.assertEqual(f.read(1), b'a') |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff
changeset
|
103 |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
104 def test_truncate(self): |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
105 if verbose: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
106 print('try truncate') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
107 with self.open(TESTFN, 'r+b') as f: |
|
45626
b932f035fb3e
Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents:
45216
diff
changeset
|
108 # 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
|
109 # 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
|
110 if not hasattr(f, 'truncate'): |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
111 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
|
112 f.seek(0, 2) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
113 # 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
|
114 self.assertEqual(f.tell(), size+1) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
115 # 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
|
116 newsize = size - 10 |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
117 f.seek(newsize) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
118 f.truncate() |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
119 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
|
120 f.seek(0, 2) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
121 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
|
122 # 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
|
123 # the file. |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
124 newsize -= 1 |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
125 f.seek(42) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
126 f.truncate(newsize) |
|
54720
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
127 if self.new_io: |
|
58624
77130f2fe944
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
128 self.assertEqual(f.tell(), 42) |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
129 f.seek(0, 2) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
130 self.assertEqual(f.tell(), newsize) |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
131 # 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
|
132 # across platform; cut it waaaaay back |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
133 f.seek(0) |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
134 f.truncate(1) |
|
54720
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
135 if self.new_io: |
|
58624
77130f2fe944
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
136 self.assertEqual(f.tell(), 0) # else pointer moved |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
137 f.seek(0) |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
138 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
|
139 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
140 def test_seekable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
141 # Issue #5016; seekable() can return False when the current position |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
142 # is negative when truncated to an int. |
|
54720
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
143 if not self.new_io: |
|
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
144 self.skipTest("builtin file doesn't have seekable()") |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
145 for pos in (2**31-1, 2**31, 2**31+1): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
146 with self.open(TESTFN, 'rb') as f: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
147 f.seek(pos) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54720
diff
changeset
|
148 self.assertTrue(f.seekable()) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
149 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
diff
changeset
|
150 |
|
45626
b932f035fb3e
Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents:
45216
diff
changeset
|
151 def test_main(): |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
152 # 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
|
153 # 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
|
154 # 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
|
155 # 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
|
156 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
|
157 requires('largefile', |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
158 '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
|
159 else: |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
160 # 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
|
161 # (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
|
162 # large files.) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
163 f = open(TESTFN, 'wb', buffering=0) |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
164 try: |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
165 # 2**31 == 2147483648 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
166 f.seek(2147483649) |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
167 # 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
|
168 # flush, too! |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
169 f.write(b'x') |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
170 f.flush() |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
171 except (IOError, OverflowError): |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
172 f.close() |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
173 unlink(TESTFN) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
174 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
|
175 else: |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
176 f.close() |
|
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
177 suite = unittest.TestSuite() |
|
54720
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
178 for _open, prefix in [(io.open, 'C'), (pyio.open, 'Py'), |
|
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
179 (open, 'Builtin')]: |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
180 class TestCase(LargeFileTest): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
181 pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
182 TestCase.open = staticmethod(_open) |
|
54720
24c8d1aabade
Re-enable testing of builtin open() in test_bufio in test_largefile
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
183 TestCase.new_io = _open is not open |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
184 TestCase.__name__ = prefix + LargeFileTest.__name__ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
185 suite.addTest(TestCase('test_seek')) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
186 suite.addTest(TestCase('test_osstat')) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
187 suite.addTest(TestCase('test_seek_read')) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
188 suite.addTest(TestCase('test_lseek')) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
189 with _open(TESTFN, 'wb') as f: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
190 if hasattr(f, 'truncate'): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
191 suite.addTest(TestCase('test_truncate')) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
192 suite.addTest(TestCase('test_seekable')) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
52355
diff
changeset
|
193 unlink(TESTFN) |
|
45626
b932f035fb3e
Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents:
45216
diff
changeset
|
194 try: |
|
b932f035fb3e
Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents:
45216
diff
changeset
|
195 run_unittest(suite) |
|
b932f035fb3e
Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents:
45216
diff
changeset
|
196 finally: |
|
b932f035fb3e
Move test_largefile over to using 'with' statements for open files.
Brett Cannon <bcannon@gmail.com>
parents:
45216
diff
changeset
|
197 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
|
198 |
|
45216
7dc9dee099e8
Fixes issue 2026. Tests converted to unittest. Thanks
Facundo Batista <facundobatista@gmail.com>
parents:
44904
diff
changeset
|
199 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
|
200 test_main() |
