diff -r b44eb630bf0a Lib/test/test_io.py --- a/Lib/test/test_io.py Mon Apr 16 21:29:58 2012 +0200 +++ b/Lib/test/test_io.py Mon Apr 16 22:51:00 2012 +0100 @@ -290,7 +290,8 @@ self.assertEqual(f.tell(), 13) self.assertEqual(f.truncate(12), 12) - self.assertEqual(f.tell(), 13) + self.assertEqual(f.truncate(size=11), 11) + self.assertEqual(f.tell(), 12) self.assertRaises(TypeError, f.seek, 0.0) def read_ops(self, f, buffered=False): diff -r b44eb630bf0a Modules/_io/fileio.c --- a/Modules/_io/fileio.c Mon Apr 16 21:29:58 2012 +0200 +++ b/Modules/_io/fileio.c Mon Apr 16 22:51:00 2012 +0100 @@ -882,9 +882,11 @@ #ifdef HAVE_FTRUNCATE static PyObject * -fileio_truncate(fileio *self, PyObject *args) +fileio_truncate(fileio *self, PyObject *args, PyObject *kwds) { - PyObject *posobj = NULL; /* the new size wanted by the user */ + static char *kwlist[] = {"size", NULL}; + PyObject *posobj = Py_None; /* the new size wanted by the user */ + PyObject *posobjk = Py_None; /* the new size wanted by the user */ #ifndef MS_WINDOWS Py_off_t pos; #endif @@ -897,9 +899,19 @@ if (!self->writable) return err_mode("writing"); - if (!PyArg_ParseTuple(args, "|O", &posobj)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", + kwlist, &posobj, &posobjk)) return NULL; + /* Fail if both positional and keyword parameters are given */ + if (posobj != Py_None && posobjk != Py_None) { + PyErr_SetString(PyExc_TypeError, "Both positional and keyword 'size' provided"); + return NULL; + } + + if (posobjk != Py_None) + posobj = posobjk; + if (posobj == Py_None || posobj == NULL) { /* Get the current position. */ posobj = portable_lseek(fd, NULL, 1); @@ -1133,16 +1145,16 @@ "writable() -> bool. True if file was opened in a write mode."); static PyMethodDef fileio_methods[] = { - {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, + {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc}, {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc}, - {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, - {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, - {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, + {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, + {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, + {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, #ifdef HAVE_FTRUNCATE - {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, + {"truncate", (PyCFunctionWithKeywords)fileio_truncate, METH_VARARGS | METH_KEYWORDS, truncate_doc}, #endif - {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, + {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc}, {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc}, {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc},