You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong tell() result for a file opened in append mode #49258
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2009-01-21.01:06:34.907>created_at=<Date2009-01-20.00:49:29.627>labels= ['type-bug', 'release-blocker']
title='Wrong tell() result for a file opened in append mode'updated_at=<Date2009-01-21.01:06:34.890>user='https://github.com/vstinner'
The example works with Python 2.x, because file object is implemented
using the FILE structure (fopen, ftell, etc.). fopen() "fixes" the
offset if the file is opened in append mode, whereas open() doesn't do
this for us :
---
you should check the error return of lseek() (and possibly wrap it in
Py_BEGIN/END_ALLOW_THREADS, see portable_lseek() in the same file)
there should be a test for each of unbuffered IO (buffering=0),
buffered IO ("rb") and text IO ("r"). For text IO, the test shouldn't
test the actual value returned by tell(), only that it is > 0 (because
tell() in text mode is an opaque value and is not necessarily equal to a
byte position)
raise raise PyErr_SetFromErrno(PyExc_IOError) on lseek() error
add tests for unbuffered binary file and (buffered) text file
I use the type "long" to store the lseek() result, because I don't
know if off_t is available on all OS. Py_off_t may be used, but it's
defined above (after fileio_init). fileio_seekable() uses the
type "int" for lseek() result, which looks worse than long :-)
I use the type "long" to store the lseek() result, because I don't
know if off_t is available on all OS. Py_off_t may be used, but it's
defined above (after fileio_init).
Instead of checking the return type, you can first set errno to 0, and
then check errno after the function returns.
fileio_seekable() uses the
type "int" for lseek() result, which looks worse than long :-)
Activity
vstinner commentedon Jan 20, 2009
The following code must display 3 instead of 0:
---
The example works with Python 2.x, because file object is implemented
using the FILE structure (fopen, ftell, etc.). fopen() "fixes" the
offset if the file is opened in append mode, whereas open() doesn't do
this for us :
---
display 0 instead of 3 on Python 2.x and 3.x.
It becomes a little bit more weird when you write something :-)
---
displays... 4 (the correct position) on Python 2.x and 3.x.
I see (in GNU libc source code) that fopen() call lseek(fd, 0,
SEEK_END) if the file is opened in append mode.
vstinner commentedon Jan 20, 2009
Patch _including a test_:
+ if (append)
+ lseek(self->fd, 0, SEEK_END);
pitrou commentedon Jan 20, 2009
Comments on the patch:
Py_BEGIN/END_ALLOW_THREADS, see portable_lseek() in the same file)
buffered IO ("rb") and text IO ("r"). For text IO, the test shouldn't
test the actual value returned by tell(), only that it is > 0 (because
tell() in text mode is an opaque value and is not necessarily equal to a
byte position)
vstinner commentedon Jan 20, 2009
Patch version 2:
I use the type "long" to store the lseek() result, because I don't
know if off_t is available on all OS. Py_off_t may be used, but it's
defined above (after fileio_init). fileio_seekable() uses the
type "int" for lseek() result, which looks worse than long :-)
pitrou commentedon Jan 20, 2009
Instead of checking the return type, you can first set errno to 0, and
then check errno after the function returns.
Nice catch! http://bugs.python.org/issue5016
PS : about the patch, "0 < f.tell()" is really strange coding style...
"f.tell() > 0" looks much more consistent with the rest of the Python
code base
pitrou commentedon Jan 20, 2009
Last thing, in your patch there is a forward declaration to
portable_lseek but it doesn't look used anywhere...
vstinner commentedon Jan 21, 2009
New try (version 3):
This patch also prepares a fix for bpo-5016.
vstinner commentedon Jan 21, 2009
Version 4: ok, let's use *portable*_lseek() instead of the "ugly"
lseek() function (not compatible with large files on Windows).
pitrou commentedon Jan 21, 2009
Committed in r68835, r68836, r68837, r68838.