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

Skip to content

Commit 66f9fea

Browse files
committed
Merged revisions 77895-77896 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r77895 | antoine.pitrou | 2010-01-31 23:47:27 +0100 (dim., 31 janv. 2010) | 12 lines 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. ........ ................ r77896 | antoine.pitrou | 2010-02-01 00:12:29 +0100 (lun., 01 févr. 2010) | 3 lines r77895 broke doctest. ................
1 parent 1665a8d commit 66f9fea

13 files changed

Lines changed: 99 additions & 48 deletions

File tree

Lib/_pyio.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ def truncate(self, pos=None):
851851
elif pos < 0:
852852
raise ValueError("negative truncate position %r" % (pos,))
853853
del self._buffer[pos:]
854-
return self.seek(pos)
854+
return pos
855855

856856
def readable(self):
857857
return True
@@ -1210,8 +1210,7 @@ def truncate(self, pos=None):
12101210
if pos is None:
12111211
pos = self.tell()
12121212
# Use seek to flush the read buffer.
1213-
self.seek(pos)
1214-
return BufferedWriter.truncate(self)
1213+
return BufferedWriter.truncate(self, pos)
12151214

12161215
def read(self, n=None):
12171216
if n is None:
@@ -1712,8 +1711,7 @@ def truncate(self, pos=None):
17121711
self.flush()
17131712
if pos is None:
17141713
pos = self.tell()
1715-
self.seek(pos)
1716-
return self.buffer.truncate()
1714+
return self.buffer.truncate(pos)
17171715

17181716
def detach(self):
17191717
if self.buffer is None:

Lib/doctest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ def getvalue(self):
247247
return result
248248

249249
def truncate(self, size=None):
250-
StringIO.truncate(self, size)
250+
self.seek(size)
251+
StringIO.truncate(self)
251252

252253
# Worst-case linear-time ellipsis matching.
253254
def _ellipsis_match(want, got):

Lib/test/test_fileio.py

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

333+
def testTruncate(self):
334+
f = _FileIO(TESTFN, 'w')
335+
f.write(bytes(bytearray(range(10))))
336+
self.assertEqual(f.tell(), 10)
337+
f.truncate(5)
338+
self.assertEqual(f.tell(), 10)
339+
self.assertEqual(f.seek(0, os.SEEK_END), 5)
340+
f.truncate(15)
341+
self.assertEqual(f.tell(), 5)
342+
self.assertEqual(f.seek(0, os.SEEK_END), 15)
343+
333344
def testTruncateOnWindows(self):
334345
def bug801631():
335346
# 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
@@ -72,7 +72,7 @@ def write_ops(self, f, t):
7272
self.assertEqual(f.seek(0), 0)
7373
self.assertEqual(f.write(t("h")), 1)
7474
self.assertEqual(f.truncate(12), 12)
75-
self.assertEqual(f.tell(), 12)
75+
self.assertEqual(f.tell(), 1)
7676

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

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Terry Carroll
118118
Donn Cave
119119
Per Cederqvist
120120
Octavian Cerna
121+
Pascal Chambon
121122
Hye-Shik Chang
122123
Jeffrey Chang
123124
Mitch Chapman

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ Core and Builtins
7676
Library
7777
-------
7878

79+
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
80+
file position when calling `truncate()`. It would previously change the
81+
file position to the given argument, which goes against the tradition of
82+
ftruncate() and other truncation APIs. Patch by Pascal Chambon.
83+
7984
- Issue #7792: Registering non-classes to ABCs raised an obscure error.
8085

8186
- Issue #7785: Don't accept bytes in FileIO.write().

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
@@ -761,8 +761,10 @@ fileio_tell(fileio *self, PyObject *args)
761761
static PyObject *
762762
fileio_truncate(fileio *self, PyObject *args)
763763
{
764-
PyObject *posobj = NULL;
764+
PyObject *posobj = NULL; /* the new size wanted by the user */
765+
#ifndef MS_WINDOWS
765766
Py_off_t pos;
767+
#endif
766768
int ret;
767769
int fd;
768770

@@ -777,58 +779,86 @@ fileio_truncate(fileio *self, PyObject *args)
777779

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

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

805813
/* Truncate. Note that this may grow the file! */
806814
Py_BEGIN_ALLOW_THREADS
807815
errno = 0;
808816
hFile = (HANDLE)_get_osfhandle(fd);
809-
ret = hFile == (HANDLE)-1;
817+
ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
810818
if (ret == 0) {
811819
ret = SetEndOfFile(hFile) == 0;
812820
if (ret)
813821
errno = EACCES;
814822
}
815823
Py_END_ALLOW_THREADS
824+
825+
/* we restore the file pointer position in any case */
826+
tempposobj = portable_lseek(fd, oldposobj, 0);
827+
Py_DECREF(oldposobj);
828+
if (tempposobj == NULL) {
829+
Py_DECREF(posobj);
830+
return NULL;
831+
}
832+
Py_DECREF(tempposobj);
816833
}
817834
#else
835+
836+
#if defined(HAVE_LARGEFILE_SUPPORT)
837+
pos = PyLong_AsLongLong(posobj);
838+
#else
839+
pos = PyLong_AsLong(posobj);
840+
#endif
841+
if (PyErr_Occurred()){
842+
Py_DECREF(posobj);
843+
return NULL;
844+
}
845+
818846
Py_BEGIN_ALLOW_THREADS
819847
errno = 0;
820848
ret = ftruncate(fd, pos);
821849
Py_END_ALLOW_THREADS
850+
822851
#endif /* !MS_WINDOWS */
823852

824853
if (ret != 0) {
854+
Py_DECREF(posobj);
825855
PyErr_SetFromErrno(PyExc_IOError);
826856
return NULL;
827857
}
828858

829859
return posobj;
830860
}
831-
#endif
861+
#endif /* HAVE_FTRUNCATE */
832862

833863
static char *
834864
mode_string(fileio *self)

0 commit comments

Comments
 (0)