Index: Lib/test/test_largefile.py =================================================================== --- Lib/test/test_largefile.py (revision 59809) +++ Lib/test/test_largefile.py (working copy) @@ -152,7 +152,7 @@ newsize -= 1 f.seek(42) f.truncate(newsize) - expect(f.tell(), 42) # else pointer moved + expect(f.tell(), newsize) # pointer should be moved f.seek(0, 2) expect(f.tell(), newsize) # else wasn't truncated @@ -161,8 +161,8 @@ # cut it waaaaay back f.seek(0) f.truncate(1) - expect(f.tell(), 0) # else pointer moved - expect(len(f.read()), 1) # else wasn't truncated + expect(f.tell(), 1) # pointer should be moved + expect(len(f.read()), 0) finally: f.close() Index: Lib/test/test_io.py =================================================================== --- Lib/test/test_io.py (revision 59809) +++ Lib/test/test_io.py (working copy) @@ -94,7 +94,7 @@ self.assertEqual(f.seek(-1, 2), 13) self.assertEqual(f.tell(), 13) self.assertEqual(f.truncate(12), 12) - self.assertEqual(f.tell(), 13) + self.assertEqual(f.tell(), 12) self.assertRaises(TypeError, f.seek, 0.0) def read_ops(self, f, buffered=False): @@ -139,7 +139,7 @@ self.assertEqual(f.tell(), 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.tell(), self.LARGE + 2) + self.assertEqual(f.tell(), self.LARGE + 1) self.assertEqual(f.seek(0, 2), self.LARGE + 1) self.assertEqual(f.seek(-1, 2), self.LARGE) self.assertEqual(f.read(2), b"x") @@ -580,6 +580,7 @@ txt.write("BB\nCCC\n") txt.write("X\rY\r\nZ") txt.flush() + self.assertEquals(buf.closed, False) self.assertEquals(buf.getvalue(), expected) def testNewlines(self): Index: Modules/_fileio.c =================================================================== --- Modules/_fileio.c (revision 59809) +++ Modules/_fileio.c (working copy) @@ -560,11 +560,10 @@ PyErr_SetString(PyExc_TypeError, "an integer is required"); return NULL; } -#if !defined(HAVE_LARGEFILE_SUPPORT) - pos = PyLong_AsLong(posobj); +#if defined(HAVE_LARGEFILE_SUPPORT) + pos = PyLong_AsLongLong(posobj); #else - pos = PyLong_Check(posobj) ? - PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); + pos = PyLong_AsLong(posobj); #endif if (PyErr_Occurred()) return NULL; @@ -580,10 +579,10 @@ if (res < 0) return PyErr_SetFromErrno(PyExc_IOError); -#if !defined(HAVE_LARGEFILE_SUPPORT) - return PyLong_FromLong(res); -#else +#if defined(HAVE_LARGEFILE_SUPPORT) return PyLong_FromLongLong(res); +#else + return PyLong_FromLong(res); #endif } @@ -630,49 +629,30 @@ return NULL; if (posobj == Py_None || posobj == NULL) { + /* Get the current position. */ posobj = portable_lseek(fd, NULL, 1); if (posobj == NULL) - return NULL; + return NULL; } else { - Py_INCREF(posobj); + /* Move to the position to be truncated. */ + posobj = portable_lseek(fd, posobj, 0); } -#if !defined(HAVE_LARGEFILE_SUPPORT) - pos = PyLong_AsLong(posobj); +#if defined(HAVE_LARGEFILE_SUPPORT) + pos = PyLong_AsLongLong(posobj); #else - pos = PyLong_Check(posobj) ? - PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); + pos = PyLong_AsLong(posobj); #endif - if (PyErr_Occurred()) { - Py_DECREF(posobj); + if (PyErr_Occurred()) return NULL; - } #ifdef MS_WINDOWS /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, so don't even try using it. */ { HANDLE hFile; - PyObject *pos2, *oldposobj; - /* store the current position */ - oldposobj = portable_lseek(self->fd, NULL, 1); - if (oldposobj == NULL) { - Py_DECREF(posobj); - return NULL; - } - - /* Have to move current pos to desired endpoint on Windows. */ - errno = 0; - pos2 = portable_lseek(fd, posobj, SEEK_SET); - if (pos2 == NULL) { - Py_DECREF(posobj); - Py_DECREF(oldposobj); - return NULL; - } - Py_DECREF(pos2); - /* Truncate. Note that this may grow the file! */ Py_BEGIN_ALLOW_THREADS errno = 0; @@ -684,18 +664,6 @@ errno = EACCES; } Py_END_ALLOW_THREADS - - if (ret == 0) { - /* Move to the previous position in the file */ - pos2 = portable_lseek(fd, oldposobj, SEEK_SET); - if (pos2 == NULL) { - Py_DECREF(posobj); - Py_DECREF(oldposobj); - return NULL; - } - } - Py_DECREF(pos2); - Py_DECREF(oldposobj); } #else Py_BEGIN_ALLOW_THREADS @@ -705,7 +673,6 @@ #endif /* !MS_WINDOWS */ if (ret != 0) { - Py_DECREF(posobj); PyErr_SetFromErrno(PyExc_IOError); return NULL; } @@ -799,7 +766,8 @@ PyDoc_STRVAR(truncate_doc, "truncate([size: int]) -> None. Truncate the file to at most size bytes.\n" "\n" -"Size defaults to the current file position, as returned by tell()."); +"Size defaults to the current file position, as returned by tell()." +"The current file position is changed to the value of size."); #endif PyDoc_STRVAR(tell_doc,