mirror of
https://github.com/python/cpython.git
synced 2025-08-16 06:40:56 +00:00
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.
This commit is contained in:
parent
f333014467
commit
ca5a06aaa9
7 changed files with 81 additions and 28 deletions
|
@ -880,7 +880,7 @@ class _BytesIO(BufferedIOBase):
|
||||||
elif pos < 0:
|
elif pos < 0:
|
||||||
raise ValueError("negative truncate position %r" % (pos,))
|
raise ValueError("negative truncate position %r" % (pos,))
|
||||||
del self._buffer[pos:]
|
del self._buffer[pos:]
|
||||||
return self.seek(pos)
|
return pos
|
||||||
|
|
||||||
def readable(self):
|
def readable(self):
|
||||||
return True
|
return True
|
||||||
|
@ -1215,8 +1215,7 @@ class BufferedRandom(BufferedWriter, BufferedReader):
|
||||||
if pos is None:
|
if pos is None:
|
||||||
pos = self.tell()
|
pos = self.tell()
|
||||||
# Use seek to flush the read buffer.
|
# Use seek to flush the read buffer.
|
||||||
self.seek(pos)
|
return BufferedWriter.truncate(self, pos)
|
||||||
return BufferedWriter.truncate(self)
|
|
||||||
|
|
||||||
def read(self, n=None):
|
def read(self, n=None):
|
||||||
if n is None:
|
if n is None:
|
||||||
|
@ -1667,8 +1666,7 @@ class TextIOWrapper(TextIOBase):
|
||||||
self.flush()
|
self.flush()
|
||||||
if pos is None:
|
if pos is None:
|
||||||
pos = self.tell()
|
pos = self.tell()
|
||||||
self.seek(pos)
|
return self.buffer.truncate(pos)
|
||||||
return self.buffer.truncate()
|
|
||||||
|
|
||||||
def seek(self, cookie, whence=0):
|
def seek(self, cookie, whence=0):
|
||||||
if self.closed:
|
if self.closed:
|
||||||
|
|
|
@ -197,6 +197,17 @@ class OtherFileTests(unittest.TestCase):
|
||||||
f.close()
|
f.close()
|
||||||
self.fail("no error for invalid mode: %s" % bad_mode)
|
self.fail("no error for invalid mode: %s" % bad_mode)
|
||||||
|
|
||||||
|
def testTruncate(self):
|
||||||
|
f = _fileio._FileIO(TESTFN, 'w')
|
||||||
|
f.write(bytes(bytearray(range(10))))
|
||||||
|
self.assertEqual(f.tell(), 10)
|
||||||
|
f.truncate(5)
|
||||||
|
self.assertEqual(f.tell(), 10)
|
||||||
|
self.assertEqual(f.seek(0, os.SEEK_END), 5)
|
||||||
|
f.truncate(15)
|
||||||
|
self.assertEqual(f.tell(), 5)
|
||||||
|
self.assertEqual(f.seek(0, os.SEEK_END), 15)
|
||||||
|
|
||||||
def testTruncateOnWindows(self):
|
def testTruncateOnWindows(self):
|
||||||
def bug801631():
|
def bug801631():
|
||||||
# SF bug <http://www.python.org/sf/801631>
|
# SF bug <http://www.python.org/sf/801631>
|
||||||
|
|
|
@ -87,6 +87,12 @@ class IOTest(unittest.TestCase):
|
||||||
test_support.unlink(test_support.TESTFN)
|
test_support.unlink(test_support.TESTFN)
|
||||||
|
|
||||||
def write_ops(self, f):
|
def write_ops(self, f):
|
||||||
|
|
||||||
|
self.assertEqual(f.write(b"blah."), 5)
|
||||||
|
f.truncate(0)
|
||||||
|
self.assertEqual(f.tell(), 5)
|
||||||
|
f.seek(0)
|
||||||
|
|
||||||
self.assertEqual(f.write(b"blah."), 5)
|
self.assertEqual(f.write(b"blah."), 5)
|
||||||
self.assertEqual(f.seek(0), 0)
|
self.assertEqual(f.seek(0), 0)
|
||||||
self.assertEqual(f.write(b"Hello."), 6)
|
self.assertEqual(f.write(b"Hello."), 6)
|
||||||
|
@ -98,8 +104,9 @@ class IOTest(unittest.TestCase):
|
||||||
self.assertEqual(f.write(b"h"), 1)
|
self.assertEqual(f.write(b"h"), 1)
|
||||||
self.assertEqual(f.seek(-1, 2), 13)
|
self.assertEqual(f.seek(-1, 2), 13)
|
||||||
self.assertEqual(f.tell(), 13)
|
self.assertEqual(f.tell(), 13)
|
||||||
|
|
||||||
self.assertEqual(f.truncate(12), 12)
|
self.assertEqual(f.truncate(12), 12)
|
||||||
self.assertEqual(f.tell(), 12)
|
self.assertEqual(f.tell(), 13)
|
||||||
self.assertRaises(TypeError, f.seek, 0.0)
|
self.assertRaises(TypeError, f.seek, 0.0)
|
||||||
|
|
||||||
def read_ops(self, f, buffered=False):
|
def read_ops(self, f, buffered=False):
|
||||||
|
@ -144,7 +151,7 @@ class IOTest(unittest.TestCase):
|
||||||
self.assertEqual(f.tell(), self.LARGE + 2)
|
self.assertEqual(f.tell(), self.LARGE + 2)
|
||||||
self.assertEqual(f.seek(0, 2), self.LARGE + 2)
|
self.assertEqual(f.seek(0, 2), self.LARGE + 2)
|
||||||
self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
|
self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
|
||||||
self.assertEqual(f.tell(), self.LARGE + 1)
|
self.assertEqual(f.tell(), self.LARGE + 2)
|
||||||
self.assertEqual(f.seek(0, 2), self.LARGE + 1)
|
self.assertEqual(f.seek(0, 2), self.LARGE + 1)
|
||||||
self.assertEqual(f.seek(-1, 2), self.LARGE)
|
self.assertEqual(f.seek(-1, 2), self.LARGE)
|
||||||
self.assertEqual(f.read(2), b"x")
|
self.assertEqual(f.read(2), b"x")
|
||||||
|
|
|
@ -32,7 +32,7 @@ class MemoryTestMixin:
|
||||||
self.assertEqual(f.seek(0), 0)
|
self.assertEqual(f.seek(0), 0)
|
||||||
self.assertEqual(f.write(t("h")), 1)
|
self.assertEqual(f.write(t("h")), 1)
|
||||||
self.assertEqual(f.truncate(12), 12)
|
self.assertEqual(f.truncate(12), 12)
|
||||||
self.assertEqual(f.tell(), 12)
|
self.assertEqual(f.tell(), 1)
|
||||||
|
|
||||||
def test_write(self):
|
def test_write(self):
|
||||||
buf = self.buftype("hello world\n")
|
buf = self.buftype("hello world\n")
|
||||||
|
@ -83,7 +83,8 @@ class MemoryTestMixin:
|
||||||
# truncate() accepts long objects
|
# truncate() accepts long objects
|
||||||
self.assertEqual(memio.truncate(4L), 4)
|
self.assertEqual(memio.truncate(4L), 4)
|
||||||
self.assertEqual(memio.getvalue(), buf[:4])
|
self.assertEqual(memio.getvalue(), buf[:4])
|
||||||
self.assertEqual(memio.tell(), 4)
|
self.assertEqual(memio.tell(), 6)
|
||||||
|
memio.seek(0, 2)
|
||||||
memio.write(buf)
|
memio.write(buf)
|
||||||
self.assertEqual(memio.getvalue(), buf[:4] + buf)
|
self.assertEqual(memio.getvalue(), buf[:4] + buf)
|
||||||
pos = memio.tell()
|
pos = memio.tell()
|
||||||
|
|
|
@ -53,6 +53,11 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- 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.
|
||||||
|
|
||||||
- Issue #7792: Registering non-classes to ABCs raised an obscure error.
|
- Issue #7792: Registering non-classes to ABCs raised an obscure error.
|
||||||
|
|
||||||
- Issue #7773: Fix an UnboundLocalError in platform.linux_distribution() when
|
- Issue #7773: Fix an UnboundLocalError in platform.linux_distribution() when
|
||||||
|
|
|
@ -445,7 +445,6 @@ bytesio_truncate(BytesIOObject *self, PyObject *args)
|
||||||
if (resize_buffer(self, size) < 0)
|
if (resize_buffer(self, size) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
self->pos = size;
|
|
||||||
|
|
||||||
return PyInt_FromSsize_t(size);
|
return PyInt_FromSsize_t(size);
|
||||||
}
|
}
|
||||||
|
|
|
@ -646,8 +646,10 @@ fileio_tell(PyFileIOObject *self, PyObject *args)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
fileio_truncate(PyFileIOObject *self, PyObject *args)
|
fileio_truncate(PyFileIOObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *posobj = NULL;
|
PyObject *posobj = NULL; /* the new size wanted by the user */
|
||||||
|
#ifndef MS_WINDOWS
|
||||||
Py_off_t pos;
|
Py_off_t pos;
|
||||||
|
#endif
|
||||||
int ret;
|
int ret;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
|
@ -667,51 +669,81 @@ fileio_truncate(PyFileIOObject *self, PyObject *args)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* Move to the position to be truncated. */
|
Py_INCREF(posobj);
|
||||||
posobj = portable_lseek(fd, posobj, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(HAVE_LARGEFILE_SUPPORT)
|
|
||||||
pos = PyLong_AsLongLong(posobj);
|
|
||||||
#else
|
|
||||||
pos = PyLong_AsLong(posobj);
|
|
||||||
#endif
|
|
||||||
if (PyErr_Occurred())
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
/* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
|
/* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
|
||||||
so don't even try using it. */
|
so don't even try using it. */
|
||||||
{
|
{
|
||||||
|
PyObject *oldposobj, *tempposobj;
|
||||||
HANDLE hFile;
|
HANDLE hFile;
|
||||||
|
|
||||||
|
/* we save the file pointer position */
|
||||||
|
oldposobj = portable_lseek(fd, NULL, 1);
|
||||||
|
if (oldposobj == NULL) {
|
||||||
|
Py_DECREF(posobj);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* we then move to the truncation position */
|
||||||
|
tempposobj = portable_lseek(fd, posobj, 0);
|
||||||
|
if (tempposobj == NULL) {
|
||||||
|
Py_DECREF(oldposobj);
|
||||||
|
Py_DECREF(posobj);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Py_DECREF(tempposobj);
|
||||||
|
|
||||||
/* Truncate. Note that this may grow the file! */
|
/* Truncate. Note that this may grow the file! */
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
errno = 0;
|
errno = 0;
|
||||||
hFile = (HANDLE)_get_osfhandle(fd);
|
hFile = (HANDLE)_get_osfhandle(fd);
|
||||||
ret = hFile == (HANDLE)-1;
|
ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = SetEndOfFile(hFile) == 0;
|
ret = SetEndOfFile(hFile) == 0;
|
||||||
if (ret)
|
if (ret)
|
||||||
errno = EACCES;
|
errno = EACCES;
|
||||||
}
|
}
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
|
|
||||||
|
/* we restore the file pointer position in any case */
|
||||||
|
tempposobj = portable_lseek(fd, oldposobj, 0);
|
||||||
|
Py_DECREF(oldposobj);
|
||||||
|
if (tempposobj == NULL) {
|
||||||
|
Py_DECREF(posobj);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Py_DECREF(tempposobj);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
#if defined(HAVE_LARGEFILE_SUPPORT)
|
||||||
|
pos = PyLong_AsLongLong(posobj);
|
||||||
|
#else
|
||||||
|
pos = PyLong_AsLong(posobj);
|
||||||
|
#endif
|
||||||
|
if (PyErr_Occurred()){
|
||||||
|
Py_DECREF(posobj);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
errno = 0;
|
errno = 0;
|
||||||
ret = ftruncate(fd, pos);
|
ret = ftruncate(fd, pos);
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
|
|
||||||
#endif /* !MS_WINDOWS */
|
#endif /* !MS_WINDOWS */
|
||||||
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
|
Py_DECREF(posobj);
|
||||||
PyErr_SetFromErrno(PyExc_IOError);
|
PyErr_SetFromErrno(PyExc_IOError);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return posobj;
|
return posobj;
|
||||||
}
|
}
|
||||||
#endif
|
#endif /* HAVE_FTRUNCATE */
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
mode_string(PyFileIOObject *self)
|
mode_string(PyFileIOObject *self)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue