summaryrefslogtreecommitdiffstats
path: root/Modules/_io/textio.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_io/textio.c')
-rw-r--r--Modules/_io/textio.c42
1 files changed, 24 insertions, 18 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 3f01980..9fb4ef9 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -224,8 +224,8 @@ typedef struct {
PyObject_HEAD
PyObject *decoder;
PyObject *errors;
- signed int pendingcr: 1;
- signed int translate: 1;
+ unsigned int pendingcr: 1;
+ unsigned int translate: 1;
unsigned int seennl: 3;
} nldecoder_object;
@@ -546,7 +546,7 @@ incrementalnewlinedecoder_setstate(nldecoder_object *self, PyObject *state)
if (!PyArg_Parse(state, "(OK)", &buffer, &flag))
return NULL;
- self->pendingcr = (int) flag & 1;
+ self->pendingcr = (int) (flag & 1);
flag >>= 1;
if (self->decoder != Py_None)
@@ -1441,6 +1441,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
PyObject *dec_buffer = NULL;
PyObject *dec_flags = NULL;
PyObject *input_chunk = NULL;
+ Py_buffer input_chunk_buf;
PyObject *decoded_chars, *chunk_size;
Py_ssize_t nbytes, nchars;
int eof;
@@ -1472,6 +1473,15 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
Py_DECREF(state);
return -1;
}
+
+ if (!PyBytes_Check(dec_buffer)) {
+ PyErr_Format(PyExc_TypeError,
+ "decoder getstate() should have returned a bytes "
+ "object, not '%.200s'",
+ Py_TYPE(dec_buffer)->tp_name);
+ Py_DECREF(state);
+ return -1;
+ }
Py_INCREF(dec_buffer);
Py_INCREF(dec_flags);
Py_DECREF(state);
@@ -1484,23 +1494,24 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint));
if (chunk_size == NULL)
goto fail;
+
input_chunk = PyObject_CallMethodObjArgs(self->buffer,
(self->has_read1 ? _PyIO_str_read1: _PyIO_str_read),
chunk_size, NULL);
Py_DECREF(chunk_size);
if (input_chunk == NULL)
goto fail;
- if (!PyBytes_Check(input_chunk)) {
+
+ if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) {
PyErr_Format(PyExc_TypeError,
- "underlying %s() should have returned a bytes object, "
+ "underlying %s() should have returned a bytes-like object, "
"not '%.200s'", (self->has_read1 ? "read1": "read"),
Py_TYPE(input_chunk)->tp_name);
goto fail;
}
- nbytes = PyBytes_Size(input_chunk);
+ nbytes = input_chunk_buf.len;
eof = (nbytes == 0);
-
if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) {
decoded_chars = _PyIncrementalNewlineDecoder_decode(
self->decoder, input_chunk, eof);
@@ -1509,6 +1520,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
decoded_chars = PyObject_CallMethodObjArgs(self->decoder,
_PyIO_str_decode, input_chunk, eof ? Py_True : Py_False, NULL);
}
+ PyBuffer_Release(&input_chunk_buf);
if (check_decoded(decoded_chars) < 0)
goto fail;
@@ -1525,18 +1537,12 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
/* At the snapshot point, len(dec_buffer) bytes before the read, the
* next input to be decoded is dec_buffer + input_chunk.
*/
- PyObject *next_input = PyNumber_Add(dec_buffer, input_chunk);
- if (next_input == NULL)
- goto fail;
- if (!PyBytes_Check(next_input)) {
- PyErr_Format(PyExc_TypeError,
- "decoder getstate() should have returned a bytes "
- "object, not '%.200s'",
- Py_TYPE(next_input)->tp_name);
- Py_DECREF(next_input);
+ PyObject *next_input = dec_buffer;
+ PyBytes_Concat(&next_input, input_chunk);
+ if (next_input == NULL) {
+ dec_buffer = NULL; /* Reference lost to PyBytes_Concat */
goto fail;
}
- Py_DECREF(dec_buffer);
Py_CLEAR(self->snapshot);
self->snapshot = Py_BuildValue("NN", dec_flags, next_input);
}
@@ -1725,7 +1731,7 @@ _PyIO_find_line_ending(
else {
/* Non-universal mode. */
Py_ssize_t readnl_len = PyUnicode_GET_LENGTH(readnl);
- char *nl = PyUnicode_DATA(readnl);
+ Py_UCS1 *nl = PyUnicode_1BYTE_DATA(readnl);
/* Assume that readnl is an ASCII character. */
assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND);
if (readnl_len == 1) {