mirror of
https://github.com/python/cpython.git
synced 2025-12-07 17:57:56 +00:00
If append mode is specified seek to the end of the file.
Add a test to test_fileio.py for this.
This commit is contained in:
parent
fee1af9d1c
commit
3a77c7ab16
2 changed files with 30 additions and 0 deletions
|
|
@ -205,6 +205,24 @@ class OtherFileTests(unittest.TestCase):
|
||||||
finally:
|
finally:
|
||||||
os.unlink(TESTFN)
|
os.unlink(TESTFN)
|
||||||
|
|
||||||
|
def testAppend(self):
|
||||||
|
try:
|
||||||
|
f = open(TESTFN, 'wb')
|
||||||
|
f.write(b'spam')
|
||||||
|
f.close()
|
||||||
|
f = open(TESTFN, 'ab')
|
||||||
|
f.write(b'eggs')
|
||||||
|
f.close()
|
||||||
|
f = open(TESTFN, 'rb')
|
||||||
|
d = f.read()
|
||||||
|
f.close()
|
||||||
|
self.assertEqual(d, b'spameggs')
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
os.unlink(TESTFN)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
# Historically, these tests have been sloppy about removing TESTFN.
|
# Historically, these tests have been sloppy about removing TESTFN.
|
||||||
# So get rid of it no matter what.
|
# So get rid of it no matter what.
|
||||||
|
|
|
||||||
|
|
@ -242,6 +242,18 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
|
||||||
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
if (append) {
|
||||||
|
int result;
|
||||||
|
Py_BEGIN_ALLOW_THREADS
|
||||||
|
errno = 0;
|
||||||
|
result = lseek(self->fd, 0, SEEK_END);
|
||||||
|
Py_END_ALLOW_THREADS
|
||||||
|
if (result < 0) {
|
||||||
|
close(self->fd);
|
||||||
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
goto done;
|
goto done;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue