gh-111178: Fix function signatures in fileio.c (#125043)

* Add "fileio_" prefix to getter functions.
* Small refactoring.
This commit is contained in:
Victor Stinner 2024-10-07 15:27:36 +02:00 committed by GitHub
parent 7487db4c7a
commit d8f707420b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 51 additions and 37 deletions

View File

@ -86,6 +86,7 @@ typedef struct {
} fileio;
#define PyFileIO_Check(state, op) (PyObject_TypeCheck((op), state->PyFileIO_Type))
#define _PyFileIO_CAST(op) _Py_CAST(fileio*, (op))
/* Forward declarations */
static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_error);
@ -93,15 +94,16 @@ static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence, bool
int
_PyFileIO_closed(PyObject *self)
{
return ((fileio *)self)->fd < 0;
return (_PyFileIO_CAST(self)->fd < 0);
}
/* Because this can call arbitrary code, it shouldn't be called when
the refcount is 0 (that is, not directly from tp_dealloc unless
the refcount has been temporarily re-incremented). */
static PyObject *
fileio_dealloc_warn(fileio *self, PyObject *source)
fileio_dealloc_warn(PyObject *op, PyObject *source)
{
fileio *self = _PyFileIO_CAST(op);
if (self->fd >= 0 && self->closefd) {
PyObject *exc = PyErr_GetRaisedException();
if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
@ -171,7 +173,7 @@ _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
exc = PyErr_GetRaisedException();
}
if (self->finalizing) {
PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
PyObject *r = fileio_dealloc_warn((PyObject*)self, (PyObject *) self);
if (r) {
Py_DECREF(r);
}
@ -192,12 +194,13 @@ _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
static PyObject *
fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
fileio *self;
assert(type != NULL && type->tp_alloc != NULL);
self = (fileio *) type->tp_alloc(type, 0);
if (self != NULL) {
fileio *self = (fileio *) type->tp_alloc(type, 0);
if (self == NULL) {
return NULL;
}
self->fd = -1;
self->created = 0;
self->readable = 0;
@ -207,8 +210,6 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->stat_atopen = NULL;
self->closefd = 1;
self->weakreflist = NULL;
}
return (PyObject *) self;
}
@ -539,36 +540,43 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
}
static int
fileio_traverse(fileio *self, visitproc visit, void *arg)
fileio_traverse(PyObject *op, visitproc visit, void *arg)
{
fileio *self = _PyFileIO_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->dict);
return 0;
}
static int
fileio_clear(fileio *self)
fileio_clear(PyObject *op)
{
fileio *self = _PyFileIO_CAST(op);
Py_CLEAR(self->dict);
return 0;
}
static void
fileio_dealloc(fileio *self)
fileio_dealloc(PyObject *op)
{
PyTypeObject *tp = Py_TYPE(self);
fileio *self = _PyFileIO_CAST(op);
self->finalizing = 1;
if (_PyIOBase_finalize((PyObject *) self) < 0)
if (_PyIOBase_finalize(op) < 0) {
return;
}
_PyObject_GC_UNTRACK(self);
if (self->stat_atopen != NULL) {
PyMem_Free(self->stat_atopen);
self->stat_atopen = NULL;
}
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
(void)fileio_clear(self);
tp->tp_free((PyObject *)self);
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs(op);
}
(void)fileio_clear(op);
PyTypeObject *tp = Py_TYPE(op);
tp->tp_free(op);
Py_DECREF(tp);
}
@ -1151,18 +1159,20 @@ mode_string(fileio *self)
}
static PyObject *
fileio_repr(fileio *self)
fileio_repr(PyObject *op)
{
PyObject *nameobj, *res;
const char *type_name = Py_TYPE((PyObject *) self)->tp_name;
fileio *self = _PyFileIO_CAST(op);
const char *type_name = Py_TYPE(self)->tp_name;
if (self->fd < 0) {
return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
}
PyObject *nameobj;
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
return NULL;
}
PyObject *res;
if (nameobj == NULL) {
res = PyUnicode_FromFormat(
"<%.100s fd=%d mode='%s' closefd=%s>",
@ -1224,7 +1234,7 @@ static PyMethodDef fileio_methods[] = {
_IO_FILEIO_WRITABLE_METHODDEF
_IO_FILEIO_FILENO_METHODDEF
_IO_FILEIO_ISATTY_METHODDEF
{"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
{"_dealloc_warn", fileio_dealloc_warn, METH_O, NULL},
{"__reduce__", _PyIOBase_cannot_pickle, METH_NOARGS},
{"__reduce_ex__", _PyIOBase_cannot_pickle, METH_O},
{NULL, NULL} /* sentinel */
@ -1233,26 +1243,30 @@ static PyMethodDef fileio_methods[] = {
/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
static PyObject *
get_closed(fileio *self, void *closure)
fileio_get_closed(PyObject *op, void *closure)
{
fileio *self = _PyFileIO_CAST(op);
return PyBool_FromLong((long)(self->fd < 0));
}
static PyObject *
get_closefd(fileio *self, void *closure)
fileio_get_closefd(PyObject *op, void *closure)
{
fileio *self = _PyFileIO_CAST(op);
return PyBool_FromLong((long)(self->closefd));
}
static PyObject *
get_mode(fileio *self, void *closure)
fileio_get_mode(PyObject *op, void *closure)
{
fileio *self = _PyFileIO_CAST(op);
return PyUnicode_FromString(mode_string(self));
}
static PyObject *
get_blksize(fileio *self, void *closure)
fileio_get_blksize(PyObject *op, void *closure)
{
fileio *self = _PyFileIO_CAST(op);
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
if (self->stat_atopen != NULL && self->stat_atopen->st_blksize > 1) {
return PyLong_FromLong(self->stat_atopen->st_blksize);
@ -1262,11 +1276,11 @@ get_blksize(fileio *self, void *closure)
}
static PyGetSetDef fileio_getsetlist[] = {
{"closed", (getter)get_closed, NULL, "True if the file is closed"},
{"closefd", (getter)get_closefd, NULL,
{"closed", fileio_get_closed, NULL, "True if the file is closed"},
{"closefd", fileio_get_closefd, NULL,
"True if the file descriptor will be closed by close()."},
{"mode", (getter)get_mode, NULL, "String giving the file mode"},
{"_blksize", (getter)get_blksize, NULL, "Stat st_blksize if available"},
{"mode", fileio_get_mode, NULL, "String giving the file mode"},
{"_blksize", fileio_get_blksize, NULL, "Stat st_blksize if available"},
{NULL},
};