Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 905a2ff

Browse files
committed
Merged revisions 77890 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r77890 | antoine.pitrou | 2010-01-31 23:26:04 +0100 (dim., 31 janv. 2010) | 7 lines - Issue #6939: Fix file I/O objects in the `io` module to keep the original file position when calling `truncate()`. It would previously change the file position to the given argument, which goes against the tradition of ftruncate() and other truncation APIs. Patch by Pascal Chambon. ........
1 parent 9b661e6 commit 905a2ff

12 files changed

Lines changed: 97 additions & 47 deletions

File tree

Lib/_pyio.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def truncate(self, pos=None):
856856
elif pos < 0:
857857
raise ValueError("negative truncate position %r" % (pos,))
858858
del self._buffer[pos:]
859-
return self.seek(pos)
859+
return pos
860860

861861
def readable(self):
862862
return True
@@ -1215,8 +1215,7 @@ def truncate(self, pos=None):
12151215
if pos is None:
12161216
pos = self.tell()
12171217
# Use seek to flush the read buffer.
1218-
self.seek(pos)
1219-
return BufferedWriter.truncate(self)
1218+
return BufferedWriter.truncate(self, pos)
12201219

12211220
def read(self, n=None):
12221221
if n is None:
@@ -1717,8 +1716,7 @@ def truncate(self, pos=None):
17171716
self.flush()
17181717
if pos is None:
17191718
pos = self.tell()
1720-
self.seek(pos)
1721-
return self.buffer.truncate()
1719+
return self.buffer.truncate(pos)
17221720

17231721
def detach(self):
17241722
if self.buffer is None:

Lib/test/test_fileio.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,17 @@ def testBadModeArgument(self):
328328
f.close()
329329
self.fail("no error for invalid mode: %s" % bad_mode)
330330

331+
def testTruncate(self):
332+
f = _FileIO(TESTFN, 'w')
333+
f.write(bytes(bytearray(range(10))))
334+
self.assertEqual(f.tell(), 10)
335+
f.truncate(5)
336+
self.assertEqual(f.tell(), 10)
337+
self.assertEqual(f.seek(0, os.SEEK_END), 5)
338+
f.truncate(15)
339+
self.assertEqual(f.tell(), 5)
340+
self.assertEqual(f.seek(0, os.SEEK_END), 15)
341+
331342
def testTruncateOnWindows(self):
332343
def bug801631():
333344
# SF bug <http://www.python.org/sf/801631>

Lib/test/test_io.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ def tearDown(self):
228228
support.unlink(support.TESTFN)
229229

230230
def write_ops(self, f):
231+
self.assertEqual(f.write(b"blah."), 5)
232+
f.truncate(0)
233+
self.assertEqual(f.tell(), 5)
234+
f.seek(0)
235+
231236
self.assertEqual(f.write(b"blah."), 5)
232237
self.assertEqual(f.seek(0), 0)
233238
self.assertEqual(f.write(b"Hello."), 6)
@@ -239,8 +244,9 @@ def write_ops(self, f):
239244
self.assertEqual(f.write(b"h"), 1)
240245
self.assertEqual(f.seek(-1, 2), 13)
241246
self.assertEqual(f.tell(), 13)
247+
242248
self.assertEqual(f.truncate(12), 12)
243-
self.assertEqual(f.tell(), 12)
249+
self.assertEqual(f.tell(), 13)
244250
self.assertRaises(TypeError, f.seek, 0.0)
245251

246252
def read_ops(self, f, buffered=False):
@@ -285,7 +291,7 @@ def large_file_ops(self, f):
285291
self.assertEqual(f.tell(), self.LARGE + 2)
286292
self.assertEqual(f.seek(0, 2), self.LARGE + 2)
287293
self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
288-
self.assertEqual(f.tell(), self.LARGE + 1)
294+
self.assertEqual(f.tell(), self.LARGE + 2)
289295
self.assertEqual(f.seek(0, 2), self.LARGE + 1)
290296
self.assertEqual(f.seek(-1, 2), self.LARGE)
291297
self.assertEqual(f.read(2), b"x")
@@ -980,7 +986,7 @@ def test_truncate(self):
980986
bufio = self.tp(raw, 8)
981987
bufio.write(b"abcdef")
982988
self.assertEqual(bufio.truncate(3), 3)
983-
self.assertEqual(bufio.tell(), 3)
989+
self.assertEqual(bufio.tell(), 6)
984990
with self.open(support.TESTFN, "rb", buffering=0) as f:
985991
self.assertEqual(f.read(), b"abc")
986992

@@ -1366,6 +1372,14 @@ def test_write_after_readahead(self):
13661372
self.assertEqual(s,
13671373
b"A" + b"B" * overwrite_size + b"A" * (9 - overwrite_size))
13681374

1375+
def test_truncate_after_read_or_write(self):
1376+
raw = self.BytesIO(b"A" * 10)
1377+
bufio = self.tp(raw, 100)
1378+
self.assertEqual(bufio.read(2), b"AA") # the read buffer gets filled
1379+
self.assertEqual(bufio.truncate(), 2)
1380+
self.assertEqual(bufio.write(b"BB"), 2) # the write buffer increases
1381+
self.assertEqual(bufio.truncate(), 4)
1382+
13691383
def test_misbehaved_io(self):
13701384
BufferedReaderTest.test_misbehaved_io(self)
13711385
BufferedWriterTest.test_misbehaved_io(self)

Lib/test/test_largefile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ def test_truncate(self):
122122
newsize -= 1
123123
f.seek(42)
124124
f.truncate(newsize)
125-
self.assertEqual(f.tell(), newsize) # else wasn't truncated
125+
self.assertEqual(f.tell(), 42)
126126
f.seek(0, 2)
127127
self.assertEqual(f.tell(), newsize)
128128
# XXX truncate(larger than true size) is ill-defined
129129
# across platform; cut it waaaaay back
130130
f.seek(0)
131131
f.truncate(1)
132-
self.assertEqual(f.tell(), 1) # else pointer moved
132+
self.assertEqual(f.tell(), 0) # else pointer moved
133133
f.seek(0)
134134
self.assertEqual(len(f.read()), 1) # else wasn't truncated
135135

Lib/test/test_memoryio.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def write_ops(self, f, t):
7373
self.assertEqual(f.seek(0), 0)
7474
self.assertEqual(f.write(t("h")), 1)
7575
self.assertEqual(f.truncate(12), 12)
76-
self.assertEqual(f.tell(), 12)
76+
self.assertEqual(f.tell(), 1)
7777

7878
def test_write(self):
7979
buf = self.buftype("hello world\n")
@@ -121,7 +121,8 @@ def test_truncate(self):
121121
self.assertEqual(memio.getvalue(), buf[:6])
122122
self.assertEqual(memio.truncate(4), 4)
123123
self.assertEqual(memio.getvalue(), buf[:4])
124-
self.assertEqual(memio.tell(), 4)
124+
self.assertEqual(memio.tell(), 6)
125+
memio.seek(0, 2)
125126
memio.write(buf)
126127
self.assertEqual(memio.getvalue(), buf[:4] + buf)
127128
pos = memio.tell()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Donn Cave
123123
Charles Cazabon
124124
Per Cederqvist
125125
Octavian Cerna
126+
Pascal Chambon
126127
Hye-Shik Chang
127128
Jeffrey Chang
128129
Mitch Chapman

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ C-API
239239
Library
240240
-------
241241

242+
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
243+
file position when calling `truncate()`. It would previously change the
244+
file position to the given argument, which goes against the tradition of
245+
ftruncate() and other truncation APIs. Patch by Pascal Chambon.
246+
242247
- Issue #7610: Reworked implementation of the internal
243248
:class:`zipfile.ZipExtFile` class used to represent files stored inside
244249
an archive. The new implementation is significantly faster and can

Modules/_io/bytesio.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ PyDoc_STRVAR(truncate_doc,
412412
"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
413413
"\n"
414414
"Size defaults to the current file position, as returned by tell().\n"
415-
"Returns the new size. Imply an absolute seek to the position size.");
415+
"The current file position is unchanged. Returns the new size.\n");
416416

417417
static PyObject *
418418
bytesio_truncate(bytesio *self, PyObject *args)
@@ -451,7 +451,6 @@ bytesio_truncate(bytesio *self, PyObject *args)
451451
if (resize_buffer(self, size) < 0)
452452
return NULL;
453453
}
454-
self->pos = size;
455454

456455
return PyLong_FromSsize_t(size);
457456
}

Modules/_io/fileio.c

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,10 @@ fileio_tell(fileio *self, PyObject *args)
758758
static PyObject *
759759
fileio_truncate(fileio *self, PyObject *args)
760760
{
761-
PyObject *posobj = NULL;
761+
PyObject *posobj = NULL; /* the new size wanted by the user */
762+
#ifndef MS_WINDOWS
762763
Py_off_t pos;
764+
#endif
763765
int ret;
764766
int fd;
765767

@@ -774,58 +776,86 @@ fileio_truncate(fileio *self, PyObject *args)
774776

775777
if (posobj == Py_None || posobj == NULL) {
776778
/* Get the current position. */
777-
posobj = portable_lseek(fd, NULL, 1);
778-
if (posobj == NULL)
779+
posobj = portable_lseek(fd, NULL, 1);
780+
if (posobj == NULL)
779781
return NULL;
780-
}
781-
else {
782-
/* Move to the position to be truncated. */
783-
posobj = portable_lseek(fd, posobj, 0);
784-
}
785-
if (posobj == NULL)
786-
return NULL;
787-
788-
#if defined(HAVE_LARGEFILE_SUPPORT)
789-
pos = PyLong_AsLongLong(posobj);
790-
#else
791-
pos = PyLong_AsLong(posobj);
792-
#endif
793-
if (pos == -1 && PyErr_Occurred())
794-
return NULL;
782+
}
783+
else {
784+
Py_INCREF(posobj);
785+
}
795786

796787
#ifdef MS_WINDOWS
797788
/* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
798789
so don't even try using it. */
799790
{
791+
PyObject *oldposobj, *tempposobj;
800792
HANDLE hFile;
793+
794+
/* we save the file pointer position */
795+
oldposobj = portable_lseek(fd, NULL, 1);
796+
if (oldposobj == NULL) {
797+
Py_DECREF(posobj);
798+
return NULL;
799+
}
800+
801+
/* we then move to the truncation position */
802+
tempposobj = portable_lseek(fd, posobj, 0);
803+
if (tempposobj == NULL) {
804+
Py_DECREF(oldposobj);
805+
Py_DECREF(posobj);
806+
return NULL;
807+
}
808+
Py_DECREF(tempposobj);
801809

802810
/* Truncate. Note that this may grow the file! */
803811
Py_BEGIN_ALLOW_THREADS
804812
errno = 0;
805813
hFile = (HANDLE)_get_osfhandle(fd);
806-
ret = hFile == (HANDLE)-1;
814+
ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
807815
if (ret == 0) {
808816
ret = SetEndOfFile(hFile) == 0;
809817
if (ret)
810818
errno = EACCES;
811819
}
812820
Py_END_ALLOW_THREADS
821+
822+
/* we restore the file pointer position in any case */
823+
tempposobj = portable_lseek(fd, oldposobj, 0);
824+
Py_DECREF(oldposobj);
825+
if (tempposobj == NULL) {
826+
Py_DECREF(posobj);
827+
return NULL;
828+
}
829+
Py_DECREF(tempposobj);
813830
}
814831
#else
832+
833+
#if defined(HAVE_LARGEFILE_SUPPORT)
834+
pos = PyLong_AsLongLong(posobj);
835+
#else
836+
pos = PyLong_AsLong(posobj);
837+
#endif
838+
if (PyErr_Occurred()){
839+
Py_DECREF(posobj);
840+
return NULL;
841+
}
842+
815843
Py_BEGIN_ALLOW_THREADS
816844
errno = 0;
817845
ret = ftruncate(fd, pos);
818846
Py_END_ALLOW_THREADS
847+
819848
#endif /* !MS_WINDOWS */
820849

821850
if (ret != 0) {
851+
Py_DECREF(posobj);
822852
PyErr_SetFromErrno(PyExc_IOError);
823853
return NULL;
824854
}
825855

826856
return posobj;
827857
}
828-
#endif
858+
#endif /* HAVE_FTRUNCATE */
829859

830860
static char *
831861
mode_string(fileio *self)

Modules/_io/iobase.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ iobase_tell(PyObject *self, PyObject *args)
102102
PyDoc_STRVAR(iobase_truncate_doc,
103103
"Truncate file to size bytes.\n"
104104
"\n"
105-
"Size defaults to the current IO position as reported by tell(). Return\n"
106-
"the new size.");
105+
"File pointer is left unchanged. Size defaults to the current IO\n"
106+
"position as reported by tell(). Returns the new size.");
107107

108108
static PyObject *
109109
iobase_truncate(PyObject *self, PyObject *args)

0 commit comments

Comments
 (0)