diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-12-13 19:30:15 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-12-13 19:30:15 (GMT) |
commit | 6b59f77c43c492f4c707ad4d8e2d2b0b871414e8 (patch) | |
tree | 68d0d1953d03c1dda5c0f9ebc8c346f63d6e70ad /Modules/_io | |
parent | bb4cd51982df53099990a11969afe57beae2d912 (diff) | |
download | cpython-6b59f77c43c492f4c707ad4d8e2d2b0b871414e8.zip cpython-6b59f77c43c492f4c707ad4d8e2d2b0b871414e8.tar.gz cpython-6b59f77c43c492f4c707ad4d8e2d2b0b871414e8.tar.bz2 |
Merged revisions 76806,76808 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r76806 | benjamin.peterson | 2009-12-13 13:25:34 -0600 (Sun, 13 Dec 2009) | 14 lines
Merged revisions 76805 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r76805 | benjamin.peterson | 2009-12-13 13:19:07 -0600 (Sun, 13 Dec 2009) | 7 lines
accept None as the same as having passed no argument in file types #7349
This is for consistency with imitation file objects like StringIO and BytesIO.
This commit also adds a few tests, where they were lacking for concerned
methods.
........
................
r76808 | benjamin.peterson | 2009-12-13 13:28:09 -0600 (Sun, 13 Dec 2009) | 9 lines
Merged revisions 76807 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r76807 | benjamin.peterson | 2009-12-13 13:27:02 -0600 (Sun, 13 Dec 2009) | 1 line
remove unused variable
........
................
Diffstat (limited to 'Modules/_io')
-rw-r--r-- | Modules/_io/_iomodule.c | 26 | ||||
-rw-r--r-- | Modules/_io/_iomodule.h | 3 | ||||
-rw-r--r-- | Modules/_io/bufferedio.c | 6 | ||||
-rw-r--r-- | Modules/_io/fileio.c | 2 | ||||
-rw-r--r-- | Modules/_io/iobase.c | 11 | ||||
-rw-r--r-- | Modules/_io/textio.c | 2 |
6 files changed, 36 insertions, 14 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 02776f3..dfd66aa 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -561,6 +561,30 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err) return result; } + +/* Basically the "n" format code with the ability to turn None into -1. */ +int +_PyIO_ConvertSsize_t(PyObject *obj, void *result) { + Py_ssize_t limit; + if (obj == Py_None) { + limit = -1; + } + else if (PyNumber_Check(obj)) { + limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError); + if (limit == -1 && PyErr_Occurred()) + return 0; + } + else { + PyErr_Format(PyExc_TypeError, + "integer argument expected, got '%.200s'", + Py_TYPE(obj)->tp_name); + return 0; + } + *((Py_ssize_t *)result) = limit; + return 1; +} + + static int iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { _PyIO_State *state = IO_MOD_STATE(mod); @@ -574,6 +598,7 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { return 0; } + static int iomodule_clear(PyObject *mod) { _PyIO_State *state = IO_MOD_STATE(mod); @@ -591,6 +616,7 @@ iomodule_free(PyObject *mod) { iomodule_clear(mod); } + /* * Module definition */ diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h index bc0e452..16ab6c2 100644 --- a/Modules/_io/_iomodule.h +++ b/Modules/_io/_iomodule.h @@ -19,6 +19,9 @@ extern PyTypeObject PyBufferedRandom_Type; extern PyTypeObject PyTextIOWrapper_Type; extern PyTypeObject PyIncrementalNewlineDecoder_Type; + +extern int _PyIO_ConvertSsize_t(PyObject *, void *); + /* These functions are used as METH_NOARGS methods, are normally called * with args=NULL, and return a new reference. * BUT when args=Py_True is passed, they return a borrowed reference. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index d8b6471..7f1bdb0 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -716,7 +716,7 @@ buffered_read(buffered *self, PyObject *args) PyObject *res; CHECK_INITIALIZED(self) - if (!PyArg_ParseTuple(args, "|n:read", &n)) { + if (!PyArg_ParseTuple(args, "|O&:read", &_PyIO_ConvertSsize_t, &n)) { return NULL; } if (n < -1) { @@ -949,10 +949,8 @@ buffered_readline(buffered *self, PyObject *args) Py_ssize_t limit = -1; CHECK_INITIALIZED(self) - - if (!PyArg_ParseTuple(args, "|n:readline", &limit)) { + if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) return NULL; - } return _buffered_readline(self, limit); } diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index f5ba971..8162396 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -602,7 +602,7 @@ fileio_read(fileio *self, PyObject *args) if (!self->readable) return err_mode("reading"); - if (!PyArg_ParseTuple(args, "|n", &size)) + if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size)) return NULL; if (size < 0) { diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 28042c6..d4757ed 100644 --- a/Modules/_io/iobase.c +++ b/Modules/_io/iobase.c @@ -455,7 +455,7 @@ iobase_readline(PyObject *self, PyObject *args) PyObject *buffer, *result; Py_ssize_t old_size = -1; - if (!PyArg_ParseTuple(args, "|n:readline", &limit)) { + if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) { return NULL; } @@ -577,16 +577,11 @@ static PyObject * iobase_readlines(PyObject *self, PyObject *args) { Py_ssize_t hint = -1, length = 0; - PyObject *hintobj = Py_None, *result; + PyObject *result; - if (!PyArg_ParseTuple(args, "|O:readlines", &hintobj)) { + if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) { return NULL; } - if (hintobj != Py_None) { - hint = PyNumber_AsSsize_t(hintobj, PyExc_ValueError); - if (hint == -1 && PyErr_Occurred()) - return NULL; - } result = PyList_New(0); if (result == NULL) diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 333c93f..ba28d59 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1479,7 +1479,7 @@ textiowrapper_read(textio *self, PyObject *args) CHECK_INITIALIZED(self); - if (!PyArg_ParseTuple(args, "|n:read", &n)) + if (!PyArg_ParseTuple(args, "|O&:read", &_PyIO_ConvertSsize_t, &n)) return NULL; CHECK_CLOSED(self); |