[Python-checkins] python/dist/src/Lib/test test_file.py,1.12,1.13

tim_one at users.sourceforge.net tim_one at users.sourceforge.net
Sat Sep 6 21:30:48 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv14962/python/Lib/test

Modified Files:
	test_file.py 
Log Message:
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.


Index: test_file.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_file.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** test_file.py	4 May 2003 04:16:50 -0000	1.12
--- test_file.py	7 Sep 2003 03:30:16 -0000	1.13
***************
*** 133,134 ****
--- 133,162 ----
  
  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)





More information about the Python-checkins mailing list