Skip to content

Commit 247afb0

Browse files
committed
refactor getters of io.StringIO
1 parent 1974630 commit 247afb0

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

Modules/_io/stringio.c

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -969,67 +969,57 @@ _io_StringIO___setstate___impl(stringio *self, PyObject *state)
969969

970970

971971
static PyObject *
972-
stringio_closed(stringio *self, void *context)
972+
stringio_closed_impl(stringio *self, void *context)
973973
{
974-
PyObject *result = NULL;
974+
CHECK_INITIALIZED(self);
975+
return PyBool_FromLong(self->closed);
976+
}
975977

978+
static PyObject *
979+
stringio_closed(stringio *self, void *context)
980+
{
981+
PyObject *result;
976982
Py_BEGIN_CRITICAL_SECTION(self);
977-
if (self->ok <= 0) {
978-
PyErr_SetString(PyExc_ValueError,
979-
"I/O operation on uninitialized object");
980-
goto exit;
981-
};
982-
result = PyBool_FromLong(self->closed);
983-
984-
exit:
983+
result = stringio_closed_impl(self, context);
985984
Py_END_CRITICAL_SECTION();
986985
return result;
987986
}
988987

989988
static PyObject *
990-
stringio_line_buffering(stringio *self, void *context)
989+
stringio_line_buffering_impl(stringio *self, void *context)
991990
{
992-
PyObject *result = NULL;
991+
CHECK_INITIALIZED(self);
992+
CHECK_CLOSED(self);
993+
Py_RETURN_FALSE;
994+
}
993995

996+
static PyObject *
997+
stringio_line_buffering(stringio *self, void *context)
998+
{
999+
PyObject *result;
9941000
Py_BEGIN_CRITICAL_SECTION(self);
995-
if (self->ok <= 0) {
996-
PyErr_SetString(PyExc_ValueError,
997-
"I/O operation on uninitialized object");
998-
goto exit;
999-
};
1000-
if (self->closed) {
1001-
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
1002-
goto exit;
1003-
};
1004-
result = Py_NewRef(Py_False);
1005-
1006-
exit:
1001+
result = stringio_line_buffering_impl(self, context);
10071002
Py_END_CRITICAL_SECTION();
10081003
return result;
10091004
}
10101005

10111006
static PyObject *
1012-
stringio_newlines(stringio *self, void *context)
1007+
stringio_newlines_impl(stringio *self, void *context)
10131008
{
1014-
PyObject *result = NULL;
1015-
1016-
Py_BEGIN_CRITICAL_SECTION(self);
1017-
if (self->ok <= 0) {
1018-
PyErr_SetString(PyExc_ValueError,
1019-
"I/O operation on uninitialized object");
1020-
goto exit;
1021-
};
1022-
if (self->closed) {
1023-
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
1024-
goto exit;
1025-
};
1009+
CHECK_INITIALIZED(self);
1010+
CHECK_CLOSED(self);
10261011
if (self->decoder == NULL) {
1027-
result = Py_NewRef(Py_None);
1028-
goto exit;
1012+
Py_RETURN_NONE;
10291013
}
1030-
result = PyObject_GetAttr(self->decoder, &_Py_ID(newlines));
1014+
return PyObject_GetAttr(self->decoder, &_Py_ID(newlines));
1015+
}
10311016

1032-
exit:
1017+
static PyObject *
1018+
stringio_newlines(stringio *self, void *context)
1019+
{
1020+
PyObject *result;
1021+
Py_BEGIN_CRITICAL_SECTION(self);
1022+
result = stringio_newlines_impl(self, context);
10331023
Py_END_CRITICAL_SECTION();
10341024
return result;
10351025
}

0 commit comments

Comments
 (0)