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

Skip to content

Commit 08838b6

Browse files
committed
Merged revisions 68835 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r68835 | antoine.pitrou | 2009-01-21 01:45:36 +0100 (mer., 21 janv. 2009) | 6 lines Issue #5008: When a file is opened in append mode with the new IO library, do an explicit seek to the end of file (so that e.g. tell() returns the file size rather than 0). This is consistent with the behaviour of the traditional 2.x file object. ........
1 parent 6268cbc commit 08838b6

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lib/test/test_io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ def test_with_open(self):
233233
else:
234234
self.fail("1/0 didn't raise an exception")
235235

236+
# issue 5008
237+
def test_append_mode_tell(self):
238+
with io.open(support.TESTFN, "wb") as f:
239+
f.write(b"xxx")
240+
with io.open(support.TESTFN, "ab", buffering=0) as f:
241+
self.assertEqual(f.tell(), 3)
242+
with io.open(support.TESTFN, "ab") as f:
243+
self.assertEqual(f.tell(), 3)
244+
with io.open(support.TESTFN, "a") as f:
245+
self.assert_(f.tell() > 0)
246+
236247
def test_destructor(self):
237248
record = []
238249
class MyFileIO(io.FileIO):

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ Core and Builtins
137137
Library
138138
-------
139139

140+
- Issue #5008: When a file is opened in append mode with the new IO library,
141+
do an explicit seek to the end of file (so that e.g. tell() returns the
142+
file size rather than 0). This is consistent with the behaviour of the
143+
traditional 2.x file object.
144+
140145
- Issue #5013: Fixed a bug in FileHandler which occurred when the delay
141146
parameter was set.
142147

Modules/_fileio.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ PyTypeObject PyFileIO_Type;
5555

5656
#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
5757

58+
static PyObject *
59+
portable_lseek(int fd, PyObject *posobj, int whence);
60+
5861
/* Returns 0 on success, -1 with exception set on failure. */
5962
static int
6063
internal_close(PyFileIOObject *self)
@@ -315,6 +318,16 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
315318
goto error;
316319
}
317320

321+
if (append) {
322+
/* For consistent behaviour, we explicitly seek to the
323+
end of file (otherwise, it might be done only on the
324+
first write()). */
325+
PyObject *pos = portable_lseek(self->fd, NULL, 2);
326+
if (pos == NULL)
327+
goto error;
328+
Py_DECREF(pos);
329+
}
330+
318331
goto done;
319332

320333
error:

0 commit comments

Comments
 (0)