mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
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.
This commit is contained in:
parent
15d529aec5
commit
fb05db2cae
4 changed files with 86 additions and 39 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue