Patch # 1323 by Amaury Forgeot d'Arc.
This patch corrects a problem in test_file.py on Windows: f.truncate() seeks to the truncation point, but does not empty the buffers. In the test, f.tell() returns -1...
This commit is contained in:
parent
687b9c0779
commit
79b79eeab2
19
Lib/io.py
19
Lib/io.py
|
@ -597,9 +597,24 @@ class _BufferedIOMixin(BufferedIOBase):
|
||||||
return self.raw.tell()
|
return self.raw.tell()
|
||||||
|
|
||||||
def truncate(self, pos=None):
|
def truncate(self, pos=None):
|
||||||
|
# On Windows, the truncate operation changes the current position
|
||||||
|
# to the end of the file, which may leave us with desynchronized
|
||||||
|
# buffers.
|
||||||
|
# Since we promise that truncate() won't change the current position,
|
||||||
|
# the easiest thing is to capture current pos now and seek back to
|
||||||
|
# it at the end.
|
||||||
|
|
||||||
|
initialpos = self.tell()
|
||||||
if pos is None:
|
if pos is None:
|
||||||
pos = self.tell()
|
pos = initialpos
|
||||||
return self.raw.truncate(pos)
|
|
||||||
|
# Flush the stream. We're mixing buffered I/O with lower-level I/O,
|
||||||
|
# and a flush may be necessary to synch both views of the current
|
||||||
|
# file state.
|
||||||
|
self.flush()
|
||||||
|
newpos = self.raw.truncate(pos)
|
||||||
|
self.seek(initialpos)
|
||||||
|
return newpos
|
||||||
|
|
||||||
### Flush and close ###
|
### Flush and close ###
|
||||||
|
|
||||||
|
|
|
@ -181,12 +181,13 @@ class OtherFileTests(unittest.TestCase):
|
||||||
self.assertEquals(d, s)
|
self.assertEquals(d, s)
|
||||||
|
|
||||||
def testTruncateOnWindows(self):
|
def testTruncateOnWindows(self):
|
||||||
os.unlink(TESTFN)
|
|
||||||
|
|
||||||
def bug801631():
|
|
||||||
# SF bug <http://www.python.org/sf/801631>
|
# SF bug <http://www.python.org/sf/801631>
|
||||||
# "file.truncate fault on windows"
|
# "file.truncate fault on windows"
|
||||||
|
|
||||||
|
os.unlink(TESTFN)
|
||||||
f = open(TESTFN, 'wb')
|
f = open(TESTFN, 'wb')
|
||||||
|
|
||||||
|
try:
|
||||||
f.write(b'12345678901') # 11 bytes
|
f.write(b'12345678901') # 11 bytes
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
@ -205,10 +206,8 @@ class OtherFileTests(unittest.TestCase):
|
||||||
size = os.path.getsize(TESTFN)
|
size = os.path.getsize(TESTFN)
|
||||||
if size != 5:
|
if size != 5:
|
||||||
self.fail("File size after ftruncate wrong %d" % size)
|
self.fail("File size after ftruncate wrong %d" % size)
|
||||||
|
|
||||||
try:
|
|
||||||
bug801631()
|
|
||||||
finally:
|
finally:
|
||||||
|
f.close()
|
||||||
os.unlink(TESTFN)
|
os.unlink(TESTFN)
|
||||||
|
|
||||||
def testIteration(self):
|
def testIteration(self):
|
||||||
|
|
Loading…
Reference in New Issue