summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2009-07-22 03:07:33 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2009-07-22 03:07:33 (GMT)
commitd2bb18b28165ccc6e4678e8046abe6ea7c9677b3 (patch)
treeedacc105f2321cdabb57bced5730c7e9d2610dfe /Modules/_io
parente671fd206b1fe31fdad7a9ec570fffe390ec56ee (diff)
downloadcpython-d2bb18b28165ccc6e4678e8046abe6ea7c9677b3.zip
cpython-d2bb18b28165ccc6e4678e8046abe6ea7c9677b3.tar.gz
cpython-d2bb18b28165ccc6e4678e8046abe6ea7c9677b3.tar.bz2
Issue #6241: Better type checking for the arguments of io.StringIO.
Diffstat (limited to 'Modules/_io')
-rw-r--r--Modules/_io/stringio.c31
1 files changed, 27 insertions, 4 deletions
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index d773723..bfb099c 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -550,22 +550,42 @@ stringio_init(stringio *self, PyObject *args, PyObject *kwds)
{
char *kwlist[] = {"initial_value", "newline", NULL};
PyObject *value = NULL;
+ PyObject *newline_obj = NULL;
char *newline = "\n";
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oz:__init__", kwlist,
- &value, &newline))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:__init__", kwlist,
+ &value, &newline_obj))
return -1;
+ /* Parse the newline argument. This used to be done with the 'z'
+ specifier, however this allowed any object with the buffer interface to
+ be converted. Thus we have to parse it manually since we only want to
+ allow unicode objects or None. */
+ if (newline_obj == Py_None) {
+ newline = NULL;
+ }
+ else if (newline_obj) {
+ if (!PyUnicode_Check(newline_obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "newline must be str or None, not %.200s",
+ Py_TYPE(newline_obj)->tp_name);
+ return -1;
+ }
+ newline = _PyUnicode_AsString(newline_obj);
+ if (newline == NULL)
+ return -1;
+ }
+
if (newline && newline[0] != '\0'
&& !(newline[0] == '\n' && newline[1] == '\0')
&& !(newline[0] == '\r' && newline[1] == '\0')
&& !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
PyErr_Format(PyExc_ValueError,
- "illegal newline value: %s", newline);
+ "illegal newline value: %R", newline_obj);
return -1;
}
if (value && value != Py_None && !PyUnicode_Check(value)) {
- PyErr_Format(PyExc_ValueError,
+ PyErr_Format(PyExc_TypeError,
"initial_value must be str or None, not %.200s",
Py_TYPE(value)->tp_name);
return -1;
@@ -577,6 +597,9 @@ stringio_init(stringio *self, PyObject *args, PyObject *kwds)
Py_CLEAR(self->writenl);
Py_CLEAR(self->decoder);
+ assert((newline != NULL && newline_obj != Py_None) ||
+ (newline == NULL && newline_obj == Py_None));
+
if (newline) {
self->readnl = PyUnicode_FromString(newline);
if (self->readnl == NULL)