Skip to content

Commit 15e972b

Browse files
committed
add critical sections to getters in StringIO
1 parent 9e183ee commit 15e972b

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

Modules/_io/stringio.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -971,28 +971,64 @@ _io_StringIO___setstate___impl(stringio *self, PyObject *state)
971971
static PyObject *
972972
stringio_closed(stringio *self, void *context)
973973
{
974-
CHECK_INITIALIZED(self);
975-
return PyBool_FromLong(self->closed);
974+
PyObject *result = NULL;
975+
976+
Py_BEGIN_CRITICAL_SECTION(self);
977+
if (self->ok <= 0) {
978+
PyErr_SetString(PyExc_ValueError,
979+
"I/O operation on uninitialized object");
980+
goto cleanup;
981+
};
982+
result = PyBool_FromLong(self->closed);
983+
984+
cleanup:
985+
Py_END_CRITICAL_SECTION();
986+
return result;
976987
}
977988

978989
static PyObject *
979990
stringio_line_buffering(stringio *self, void *context)
980991
{
981-
CHECK_INITIALIZED(self);
982-
CHECK_CLOSED(self);
983-
Py_RETURN_FALSE;
992+
PyObject *result = NULL;
993+
994+
Py_BEGIN_CRITICAL_SECTION(self);
995+
if (self->ok <= 0) {
996+
PyErr_SetString(PyExc_ValueError,
997+
"I/O operation on uninitialized object");
998+
goto cleanup;
999+
};
1000+
if (self->closed) {
1001+
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
1002+
goto cleanup;
1003+
};
1004+
result = Py_NewRef(Py_False);
1005+
1006+
cleanup:
1007+
Py_END_CRITICAL_SECTION();
1008+
return result;
9841009
}
9851010

9861011
static PyObject *
9871012
stringio_newlines(stringio *self, void *context)
9881013
{
989-
CHECK_INITIALIZED(self);
990-
CHECK_CLOSED(self);
991-
if (self->decoder == NULL)
992-
Py_RETURN_NONE;
9931014
PyObject *result = NULL;
1015+
9941016
Py_BEGIN_CRITICAL_SECTION(self);
1017+
if (self->ok <= 0) {
1018+
PyErr_SetString(PyExc_ValueError,
1019+
"I/O operation on uninitialized object");
1020+
goto cleanup;
1021+
};
1022+
if (self->closed) {
1023+
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
1024+
goto cleanup;
1025+
};
1026+
if (self->decoder == NULL) {
1027+
goto cleanup;
1028+
}
9951029
result = PyObject_GetAttr(self->decoder, &_Py_ID(newlines));
1030+
1031+
cleanup:
9961032
Py_END_CRITICAL_SECTION();
9971033
return result;
9981034
}

0 commit comments

Comments
 (0)