diff options
author | Tim Peters <tim.peters@gmail.com> | 2003-09-07 03:30:18 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2003-09-07 03:30:18 (GMT) |
commit | f1827cfaab46e4d9137fd72543c83977eba50cb0 (patch) | |
tree | a9eff73415e1950bd2448402d3d2db842d66cad0 /Lib/test/test_file.py | |
parent | a26c16c821461986e08ddd63a60da42150180606 (diff) | |
download | cpython-f1827cfaab46e4d9137fd72543c83977eba50cb0.zip cpython-f1827cfaab46e4d9137fd72543c83977eba50cb0.tar.gz cpython-f1827cfaab46e4d9137fd72543c83977eba50cb0.tar.bz2 |
SF bug 801631: file.truncate fault on windows.
file_truncate(): C doesn't define what fflush(fp) does if fp is open
for update, and the preceding I/O operation on fp was input. On Windows,
fflush() actually changes the current file position then. Because
Windows doesn't support ftruncate() directly, this not only caused
Python's file.truncate() to change the file position (contra our docs),
it also caused the file not to change size.
Repaired by getting the initial file position at the start, restoring
it at the end, and tossing all the complicated micro-efficiency checks
trying to avoid "provably unnecessary" seeks. file.truncate() can't
be a frequent operation, and seeking to the current file position has
got to be cheap anyway.
Bugfix candidate.
Diffstat (limited to 'Lib/test/test_file.py')
-rw-r--r-- | Lib/test/test_file.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index 677dafc..b8bcab7 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -132,3 +132,31 @@ else: raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError' os.unlink(TESTFN) + +def bug801631(): + # SF bug <http://www.python.org/sf/801631> + # "file.truncate fault on windows" + f = file(TESTFN, 'wb') + f.write('12345678901') # 11 bytes + f.close() + + f = file(TESTFN,'rb+') + data = f.read(5) + if data != '12345': + raise TestFailed("Read on file opened for update failed %r" % data) + if f.tell() != 5: + raise TestFailed("File pos after read wrong %d" % f.tell()) + + f.truncate() + if f.tell() != 5: + raise TestFailed("File pos after ftruncate wrong %d" % f.tell()) + + f.close() + size = os.path.getsize(TESTFN) + if size != 5: + raise TestFailed("File size after ftruncate wrong %d" % size) + +try: + bug801631() +finally: + os.unlink(TESTFN) |