summaryrefslogtreecommitdiff
path: root/Lib/test/test_largefile.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2002-03-11 00:24:00 +0000
committerTim Peters <tim.peters@gmail.com>2002-03-11 00:24:00 +0000
commitfb05db2cae72bb0c3f2181d6cb0c20d990c23f6c (patch)
treee92554dd247a501279c7ff742689f2521e60e34e /Lib/test/test_largefile.py
parent15d529aec52276fe4df83aeda5b01e8df77344dc (diff)
downloadcpython-git-fb05db2cae72bb0c3f2181d6cb0c20d990c23f6c.tar.gz
file_truncate(): provide full "large file" support on Windows, by
dropping MS's inadequate _chsize() function. This was inspired by SF patch 498109 ("fileobject truncate support for win32"), which I rejected. libstdtypes.tex: Someone who knows should update the availability blurb. For example, if it's available on Linux, it would be good to say so. test_largefile: Uncommented the file.truncate() tests, and reworked to do more. The old comment about "permission errors" in the truncation tests under Windows was almost certainly due to that the file wasn't open for *write* access at this point, so of course MS wouldn't let you truncate it. I'd be appalled if a Unixish system did. CAUTION: Someone should run this test on Linux (etc) too. The truncation part was commented out before. Note that test_largefile isn't run by default.
Diffstat (limited to 'Lib/test/test_largefile.py')
-rw-r--r--Lib/test/test_largefile.py39
1 files changed, 24 insertions, 15 deletions
diff --git a/Lib/test/test_largefile.py b/Lib/test/test_largefile.py
index abfee39e98..bc246353b5 100644
--- a/Lib/test/test_largefile.py
+++ b/Lib/test/test_largefile.py
@@ -128,20 +128,29 @@ expect(os.lseek(f.fileno(), size, 0), size)
expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
f.close()
-
-# XXX add tests for truncate if it exists
-# XXX has truncate ever worked on Windows? specifically on WinNT I get:
-# "IOError: [Errno 13] Permission denied"
-##try:
-## newsize = size - 10
-## f.seek(newsize)
-## f.truncate()
-## expect(f.tell(), newsize)
-## newsize = newsize - 1
-## f.seek(0)
-## f.truncate(newsize)
-## expect(f.tell(), newsize)
-##except AttributeError:
-## pass
+if hasattr(f, 'truncate'):
+ if test_support.verbose:
+ print 'try truncate'
+ f = open(name, 'r+b')
+ f.seek(0, 2)
+ expect(f.tell(), size+1)
+ # Cut it back via seek + truncate with no argument.
+ newsize = size - 10
+ f.seek(newsize)
+ f.truncate()
+ expect(f.tell(), newsize)
+ # Ensure that truncate(bigger than true size) doesn't grow the file.
+ f.truncate(size)
+ expect(f.tell(), newsize)
+ # Ensure that truncate(smaller than true size) shrinks the file.
+ newsize -= 1
+ f.seek(0)
+ f.truncate(newsize)
+ expect(f.tell(), newsize)
+ # cut it waaaaay back
+ f.truncate(1)
+ f.seek(0)
+ expect(len(f.read()), 1)
+ f.close()
os.unlink(name)