From 0deab5bfac791b3c698e09dc885003daa5cf42fe Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 24 Aug 2017 13:10:06 +0300 Subject: [PATCH 1/2] init commit --- Lib/test/test_io.py | 2 ++ Modules/_io/textio.c | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index ab0cbe163613ec..7eb4ae0eda4b8e 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -3245,6 +3245,8 @@ def _make_illegal_wrapper(): t = _make_illegal_wrapper() self.assertRaises(TypeError, t.read) + # Make sure TypeError (and not SystemError) is raised + def _check_create_at_shutdown(self, **kwargs): # Issue #20037: creating a TextIOWrapper at shutdown # shouldn't crash the interpreter. diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 57b66d66c33745..ae07d87b2e97b5 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -531,7 +531,15 @@ _io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self) _PyIO_str_getstate, NULL); if (state == NULL) return NULL; - if (!PyArg_ParseTuple(state, "OK", &buffer, &flag)) { + if (!PyTuple_Check(state)) { + PyErr_SetString(PyExc_TypeError, + "illegal decoder state"); + Py_DECREF(state); + return NULL; + } + if (!PyArg_ParseTuple(state, "OK;illegal decoder state", + &buffer, &flag)) + { Py_DECREF(state); return NULL; } @@ -2378,7 +2386,15 @@ _io_TextIOWrapper_tell_impl(textio *self) _PyIO_str_getstate, NULL); \ if (_state == NULL) \ goto fail; \ - if (!PyArg_ParseTuple(_state, "Oi", &dec_buffer, &dec_flags)) { \ + if (!PyTuple_Check(_state)) { \ + PyErr_SetString(PyExc_TypeError, \ + "illegal decoder state"); \ + Py_DECREF(_state); \ + goto fail; \ + } \ + if (!PyArg_ParseTuple(_state, "Oi;illegal decoder state", \ + &dec_buffer, &dec_flags)) \ + { \ Py_DECREF(_state); \ goto fail; \ } \ From ba3daf86700e50c7ce37e896838a469324e074bd Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 24 Aug 2017 16:43:04 +0300 Subject: [PATCH 2/2] add an assert and fix a comment. --- Lib/test/test_io.py | 2 -- Modules/_io/textio.c | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 7eb4ae0eda4b8e..ab0cbe163613ec 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -3245,8 +3245,6 @@ def _make_illegal_wrapper(): t = _make_illegal_wrapper() self.assertRaises(TypeError, t.read) - # Make sure TypeError (and not SystemError) is raised - def _check_create_at_shutdown(self, **kwargs): # Issue #20037: creating a TextIOWrapper at shutdown # shouldn't crash the interpreter. diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index ae07d87b2e97b5..5103ae659ce7de 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -677,7 +677,7 @@ typedef struct written, or NULL */ Py_ssize_t pending_bytes_count; - /* snapshot is either None, or a tuple (dec_flags, next_input) where + /* snapshot is either NULL, or a tuple (dec_flags, next_input) where * dec_flags is the second (integer) item of the decoder state and * next_input is the chunk of input bytes that comes next after the * snapshot point. We use this to reconstruct decoder states in tell(). @@ -2359,6 +2359,7 @@ _io_TextIOWrapper_tell_impl(textio *self) goto fail; /* Skip backward to the snapshot point (see _read_chunk). */ + assert(PyTuple_Check(self->snapshot)); if (!PyArg_ParseTuple(self->snapshot, "iO", &cookie.dec_flags, &next_input)) goto fail;