diff -r cac63f6ad252 Lib/test/test_io.py --- a/Lib/test/test_io.py Mon Apr 16 21:34:24 2012 -0600 +++ b/Lib/test/test_io.py Tue Apr 17 10:23:45 2012 +0100 @@ -282,6 +282,7 @@ self.assertEqual(f.write(b"Hello."), 6) self.assertEqual(f.tell(), 6) self.assertEqual(f.seek(-1, 1), 5) + self.assertEqual(f.seek(-1, whence=1), 5) self.assertEqual(f.tell(), 5) self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9) self.assertEqual(f.seek(0), 0) @@ -291,6 +292,8 @@ 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): @@ -399,6 +402,7 @@ self.assertEqual(f.readline(), b"abc\n") self.assertEqual(f.readline(10), b"def\n") self.assertEqual(f.readline(2), b"xy") + self.assertEqual(f.readline(limit=2), b"xy") self.assertEqual(f.readline(4), b"zzy\n") self.assertEqual(f.readline(), b"foo\x00bar\n") self.assertEqual(f.readline(None), b"another line") @@ -857,6 +861,7 @@ return self.tp(rawio) self.assertEqual(bufio().readlines(), [b"abc\n", b"d\n", b"ef"]) self.assertEqual(bufio().readlines(5), [b"abc\n", b"d\n"]) + self.assertEqual(bufio().readlines(hint=5), [b"abc\n", b"d\n"]) self.assertEqual(bufio().readlines(None), [b"abc\n", b"d\n", b"ef"]) def test_buffering(self): diff -r cac63f6ad252 Modules/_io/fileio.c --- a/Modules/_io/fileio.c Mon Apr 16 21:34:24 2012 -0600 +++ b/Modules/_io/fileio.c Tue Apr 17 10:23:45 2012 +0100 @@ -686,19 +686,22 @@ } static PyObject * -fileio_read(fileio *self, PyObject *args) +fileio_read(fileio *self, PyObject *args, PyObject *kwds) { char *ptr; Py_ssize_t n; Py_ssize_t size = -1; PyObject *bytes; + static char *kwlist[] = {"n", NULL}; if (self->fd < 0) return err_closed(); if (!self->readable) return err_mode("reading"); - if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, + "|O&", kwlist, + &_PyIO_ConvertSsize_t, &size)) return NULL; if (size < 0) { @@ -857,15 +860,18 @@ } static PyObject * -fileio_seek(fileio *self, PyObject *args) +fileio_seek(fileio *self, PyObject *args, PyObject *kwds) { PyObject *posobj; int whence = 0; + static char *kwlist[] = {"offset", "whence", NULL}; if (self->fd < 0) return err_closed(); - if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, + "O|i", kwlist, + &posobj, &whence)) return NULL; return portable_lseek(self->fd, posobj, whence); @@ -882,9 +888,10 @@ #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}; #ifndef MS_WINDOWS Py_off_t pos; #endif @@ -897,7 +904,7 @@ if (!self->writable) return err_mode("writing"); - if (!PyArg_ParseTuple(args, "|O", &posobj)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &posobj)) return NULL; if (posobj == Py_None || posobj == NULL) { @@ -1133,16 +1140,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 | METH_KEYWORDS, seek_doc}, + {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, #ifdef HAVE_FTRUNCATE - {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, + {"truncate", (PyCFunction)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}, @@ -1150,7 +1157,7 @@ {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc}, {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL}, {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL}, - {NULL, NULL} /* sentinel */ + {NULL, NULL, 0, NULL} /* sentinel */ }; /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */ diff -r cac63f6ad252 Modules/_io/iobase.c --- a/Modules/_io/iobase.c Mon Apr 16 21:34:24 2012 -0600 +++ b/Modules/_io/iobase.c Tue Apr 17 10:23:45 2012 +0100 @@ -87,7 +87,7 @@ "Return the new absolute position."); static PyObject * -iobase_seek(PyObject *self, PyObject *args) +iobase_seek(PyObject *self, PyObject *args, PyObject *kwds) { return iobase_unsupported("seek"); } @@ -110,7 +110,7 @@ "position as reported by tell(). Returns the new size."); static PyObject * -iobase_truncate(PyObject *self, PyObject *args) +iobase_truncate(PyObject *self, PyObject *args, PyObject *kwds) { return iobase_unsupported("truncate"); } @@ -447,10 +447,11 @@ "terminator(s) recognized.\n"); static PyObject * -iobase_readline(PyObject *self, PyObject *args) +iobase_readline(PyObject *self, PyObject *args, PyObject *kwds) { /* For backwards compatibility, a (slowish) readline(). */ + static char *kwlist[] = {"limit", NULL}; Py_ssize_t limit = -1; int has_peek = 0; PyObject *buffer, *result; @@ -458,7 +459,9 @@ _Py_IDENTIFIER(read); _Py_IDENTIFIER(peek); - if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, + "|O&:readline", kwlist, + &_PyIO_ConvertSsize_t, &limit)) { return NULL; } @@ -579,12 +582,15 @@ "lines so far exceeds hint."); static PyObject * -iobase_readlines(PyObject *self, PyObject *args) +iobase_readlines(PyObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = {"hint", NULL}; Py_ssize_t hint = -1, length = 0; PyObject *result; - if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, + "|O&:readlines", kwlist, + &_PyIO_ConvertSsize_t, &hint)) { return NULL; } @@ -672,11 +678,11 @@ } static PyMethodDef iobase_methods[] = { - {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, - {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc}, - {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, - {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc}, - {"close", iobase_close, METH_NOARGS, iobase_close_doc}, + {"seek", iobase_seek, METH_VARARGS | METH_KEYWORDS, iobase_seek_doc}, + {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc}, + {"truncate", iobase_truncate, METH_VARARGS | METH_KEYWORDS, iobase_truncate_doc}, + {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc}, + {"close", iobase_close, METH_NOARGS, iobase_close_doc}, {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc}, {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc}, @@ -693,8 +699,8 @@ {"__enter__", iobase_enter, METH_NOARGS}, {"__exit__", iobase_exit, METH_VARARGS}, - {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc}, - {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc}, + {"readline", iobase_readline, METH_VARARGS | METH_KEYWORDS, iobase_readline_doc}, + {"readlines", iobase_readlines, METH_VARARGS | METH_KEYWORDS, iobase_readlines_doc}, {"writelines", iobase_writelines, METH_VARARGS}, {NULL, NULL}