summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-01-31 23:20:26 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-01-31 23:20:26 (GMT)
commit66f9fea782d330b28fe142ac4e58faff8a6f42d1 (patch)
tree664127f167fb8d906cf7f7de7d1e1557fea0e647 /Modules/_io
parent1665a8d9313660ffdcf3eda3eb1e1cc4eefcacee (diff)
downloadcpython-66f9fea782d330b28fe142ac4e58faff8a6f42d1.zip
cpython-66f9fea782d330b28fe142ac4e58faff8a6f42d1.tar.gz
cpython-66f9fea782d330b28fe142ac4e58faff8a6f42d1.tar.bz2
Merged revisions 77895-77896 via svnmerge from
svn+ssh://pythondev@svn.python.org/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://pythondev@svn.python.org/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. ................
Diffstat (limited to 'Modules/_io')
-rw-r--r--Modules/_io/bytesio.c3
-rw-r--r--Modules/_io/fileio.c70
-rw-r--r--Modules/_io/iobase.c4
-rw-r--r--Modules/_io/stringio.c3
-rw-r--r--Modules/_io/textio.c10
5 files changed, 55 insertions, 35 deletions
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index e3a7849..686e20a 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -412,7 +412,7 @@ PyDoc_STRVAR(truncate_doc,
"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
"\n"
"Size defaults to the current file position, as returned by tell().\n"
-"Returns the new size. Imply an absolute seek to the position size.");
+"The current file position is unchanged. Returns the new size.\n");
static PyObject *
bytesio_truncate(bytesio *self, PyObject *args)
@@ -451,7 +451,6 @@ bytesio_truncate(bytesio *self, PyObject *args)
if (resize_buffer(self, size) < 0)
return NULL;
}
- self->pos = size;
return PyLong_FromSsize_t(size);
}
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index d04a74d..81e4019 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -761,8 +761,10 @@ fileio_tell(fileio *self, PyObject *args)
static PyObject *
fileio_truncate(fileio *self, PyObject *args)
{
- PyObject *posobj = NULL;
+ PyObject *posobj = NULL; /* the new size wanted by the user */
+#ifndef MS_WINDOWS
Py_off_t pos;
+#endif
int ret;
int fd;
@@ -777,58 +779,86 @@ fileio_truncate(fileio *self, PyObject *args)
if (posobj == Py_None || posobj == NULL) {
/* Get the current position. */
- posobj = portable_lseek(fd, NULL, 1);
- if (posobj == NULL)
+ posobj = portable_lseek(fd, NULL, 1);
+ if (posobj == NULL)
return NULL;
- }
- else {
- /* Move to the position to be truncated. */
- posobj = portable_lseek(fd, posobj, 0);
- }
- if (posobj == NULL)
- return NULL;
-
-#if defined(HAVE_LARGEFILE_SUPPORT)
- pos = PyLong_AsLongLong(posobj);
-#else
- pos = PyLong_AsLong(posobj);
-#endif
- if (pos == -1 && PyErr_Occurred())
- return NULL;
+ }
+ else {
+ Py_INCREF(posobj);
+ }
#ifdef MS_WINDOWS
/* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
so don't even try using it. */
{
+ PyObject *oldposobj, *tempposobj;
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! */
Py_BEGIN_ALLOW_THREADS
errno = 0;
hFile = (HANDLE)_get_osfhandle(fd);
- ret = hFile == (HANDLE)-1;
+ ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
if (ret == 0) {
ret = SetEndOfFile(hFile) == 0;
if (ret)
errno = EACCES;
}
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
+
+#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
errno = 0;
ret = ftruncate(fd, pos);
Py_END_ALLOW_THREADS
+
#endif /* !MS_WINDOWS */
if (ret != 0) {
+ Py_DECREF(posobj);
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return posobj;
}
-#endif
+#endif /* HAVE_FTRUNCATE */
static char *
mode_string(fileio *self)
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index d4757ed..271a41e 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -102,8 +102,8 @@ iobase_tell(PyObject *self, PyObject *args)
PyDoc_STRVAR(iobase_truncate_doc,
"Truncate file to size bytes.\n"
"\n"
- "Size defaults to the current IO position as reported by tell(). Return\n"
- "the new size.");
+ "File pointer is left unchanged. Size defaults to the current IO\n"
+ "position as reported by tell(). Returns the new size.");
static PyObject *
iobase_truncate(PyObject *self, PyObject *args)
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index bfb099c..e02d26b 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -350,7 +350,7 @@ PyDoc_STRVAR(stringio_truncate_doc,
"Truncate size to pos.\n"
"\n"
"The pos argument defaults to the current file position, as\n"
- "returned by tell(). Imply an absolute seek to pos.\n"
+ "returned by tell(). The current file position is unchanged.\n"
"Returns the new absolute position.\n");
static PyObject *
@@ -390,7 +390,6 @@ stringio_truncate(stringio *self, PyObject *args)
return NULL;
self->string_size = size;
}
- self->pos = size;
return PyLong_FromSsize_t(size);
}
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index ba28d59..89d922c 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2318,15 +2318,7 @@ textiowrapper_truncate(textio *self, PyObject *args)
return NULL;
Py_DECREF(res);
- if (pos != Py_None) {
- res = PyObject_CallMethodObjArgs((PyObject *) self,
- _PyIO_str_seek, pos, NULL);
- if (res == NULL)
- return NULL;
- Py_DECREF(res);
- }
-
- return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, NULL);
+ return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, pos, NULL);
}
static PyObject *