@@ -86,22 +86,24 @@ typedef struct {
8686} fileio ;
8787
8888#define PyFileIO_Check (state , op ) (PyObject_TypeCheck((op), state->PyFileIO_Type))
89+ #define _PyFileIO_CAST (op ) _Py_CAST(fileio*, (op))
8990
9091/* Forward declarations */
9192static PyObject * portable_lseek (fileio * self , PyObject * posobj , int whence , bool suppress_pipe_error );
9293
9394int
9495_PyFileIO_closed (PyObject * self )
9596{
96- return (( fileio * ) self )-> fd < 0 ;
97+ return (_PyFileIO_CAST ( self )-> fd < 0 ) ;
9798}
9899
99100/* Because this can call arbitrary code, it shouldn't be called when
100101 the refcount is 0 (that is, not directly from tp_dealloc unless
101102 the refcount has been temporarily re-incremented). */
102103static PyObject *
103- fileio_dealloc_warn (fileio * self , PyObject * source )
104+ fileio_dealloc_warn (PyObject * op , PyObject * source )
104105{
106+ fileio * self = _PyFileIO_CAST (op );
105107 if (self -> fd >= 0 && self -> closefd ) {
106108 PyObject * exc = PyErr_GetRaisedException ();
107109 if (PyErr_ResourceWarning (source , 1 , "unclosed file %R" , source )) {
@@ -171,7 +173,7 @@ _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
171173 exc = PyErr_GetRaisedException ();
172174 }
173175 if (self -> finalizing ) {
174- PyObject * r = fileio_dealloc_warn (self , (PyObject * ) self );
176+ PyObject * r = fileio_dealloc_warn (( PyObject * ) self , (PyObject * ) self );
175177 if (r ) {
176178 Py_DECREF (r );
177179 }
@@ -192,23 +194,22 @@ _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
192194static PyObject *
193195fileio_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
194196{
195- fileio * self ;
196-
197197 assert (type != NULL && type -> tp_alloc != NULL );
198198
199- self = (fileio * ) type -> tp_alloc (type , 0 );
200- if (self != NULL ) {
201- self -> fd = -1 ;
202- self -> created = 0 ;
203- self -> readable = 0 ;
204- self -> writable = 0 ;
205- self -> appending = 0 ;
206- self -> seekable = -1 ;
207- self -> stat_atopen = NULL ;
208- self -> closefd = 1 ;
209- self -> weakreflist = NULL ;
199+ fileio * self = (fileio * ) type -> tp_alloc (type , 0 );
200+ if (self == NULL ) {
201+ return NULL ;
210202 }
211203
204+ self -> fd = -1 ;
205+ self -> created = 0 ;
206+ self -> readable = 0 ;
207+ self -> writable = 0 ;
208+ self -> appending = 0 ;
209+ self -> seekable = -1 ;
210+ self -> stat_atopen = NULL ;
211+ self -> closefd = 1 ;
212+ self -> weakreflist = NULL ;
212213 return (PyObject * ) self ;
213214}
214215
@@ -539,36 +540,43 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
539540}
540541
541542static int
542- fileio_traverse (fileio * self , visitproc visit , void * arg )
543+ fileio_traverse (PyObject * op , visitproc visit , void * arg )
543544{
545+ fileio * self = _PyFileIO_CAST (op );
544546 Py_VISIT (Py_TYPE (self ));
545547 Py_VISIT (self -> dict );
546548 return 0 ;
547549}
548550
549551static int
550- fileio_clear (fileio * self )
552+ fileio_clear (PyObject * op )
551553{
554+ fileio * self = _PyFileIO_CAST (op );
552555 Py_CLEAR (self -> dict );
553556 return 0 ;
554557}
555558
556559static void
557- fileio_dealloc (fileio * self )
560+ fileio_dealloc (PyObject * op )
558561{
559- PyTypeObject * tp = Py_TYPE ( self );
562+ fileio * self = _PyFileIO_CAST ( op );
560563 self -> finalizing = 1 ;
561- if (_PyIOBase_finalize (( PyObject * ) self ) < 0 )
564+ if (_PyIOBase_finalize (op ) < 0 ) {
562565 return ;
566+ }
567+
563568 _PyObject_GC_UNTRACK (self );
564569 if (self -> stat_atopen != NULL ) {
565570 PyMem_Free (self -> stat_atopen );
566571 self -> stat_atopen = NULL ;
567572 }
568- if (self -> weakreflist != NULL )
569- PyObject_ClearWeakRefs ((PyObject * ) self );
570- (void )fileio_clear (self );
571- tp -> tp_free ((PyObject * )self );
573+ if (self -> weakreflist != NULL ) {
574+ PyObject_ClearWeakRefs (op );
575+ }
576+ (void )fileio_clear (op );
577+
578+ PyTypeObject * tp = Py_TYPE (op );
579+ tp -> tp_free (op );
572580 Py_DECREF (tp );
573581}
574582
@@ -1151,18 +1159,20 @@ mode_string(fileio *self)
11511159}
11521160
11531161static PyObject *
1154- fileio_repr (fileio * self )
1162+ fileio_repr (PyObject * op )
11551163{
1156- PyObject * nameobj , * res ;
1157- const char * type_name = Py_TYPE (( PyObject * ) self )-> tp_name ;
1164+ fileio * self = _PyFileIO_CAST ( op ) ;
1165+ const char * type_name = Py_TYPE (self )-> tp_name ;
11581166
11591167 if (self -> fd < 0 ) {
11601168 return PyUnicode_FromFormat ("<%.100s [closed]>" , type_name );
11611169 }
11621170
1171+ PyObject * nameobj ;
11631172 if (PyObject_GetOptionalAttr ((PyObject * ) self , & _Py_ID (name ), & nameobj ) < 0 ) {
11641173 return NULL ;
11651174 }
1175+ PyObject * res ;
11661176 if (nameobj == NULL ) {
11671177 res = PyUnicode_FromFormat (
11681178 "<%.100s fd=%d mode='%s' closefd=%s>" ,
@@ -1224,7 +1234,7 @@ static PyMethodDef fileio_methods[] = {
12241234 _IO_FILEIO_WRITABLE_METHODDEF
12251235 _IO_FILEIO_FILENO_METHODDEF
12261236 _IO_FILEIO_ISATTY_METHODDEF
1227- {"_dealloc_warn" , ( PyCFunction ) fileio_dealloc_warn , METH_O , NULL },
1237+ {"_dealloc_warn" , fileio_dealloc_warn , METH_O , NULL },
12281238 {"__reduce__" , _PyIOBase_cannot_pickle , METH_NOARGS },
12291239 {"__reduce_ex__" , _PyIOBase_cannot_pickle , METH_O },
12301240 {NULL , NULL } /* sentinel */
@@ -1233,26 +1243,30 @@ static PyMethodDef fileio_methods[] = {
12331243/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
12341244
12351245static PyObject *
1236- get_closed ( fileio * self , void * closure )
1246+ fileio_get_closed ( PyObject * op , void * closure )
12371247{
1248+ fileio * self = _PyFileIO_CAST (op );
12381249 return PyBool_FromLong ((long )(self -> fd < 0 ));
12391250}
12401251
12411252static PyObject *
1242- get_closefd ( fileio * self , void * closure )
1253+ fileio_get_closefd ( PyObject * op , void * closure )
12431254{
1255+ fileio * self = _PyFileIO_CAST (op );
12441256 return PyBool_FromLong ((long )(self -> closefd ));
12451257}
12461258
12471259static PyObject *
1248- get_mode ( fileio * self , void * closure )
1260+ fileio_get_mode ( PyObject * op , void * closure )
12491261{
1262+ fileio * self = _PyFileIO_CAST (op );
12501263 return PyUnicode_FromString (mode_string (self ));
12511264}
12521265
12531266static PyObject *
1254- get_blksize ( fileio * self , void * closure )
1267+ fileio_get_blksize ( PyObject * op , void * closure )
12551268{
1269+ fileio * self = _PyFileIO_CAST (op );
12561270#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
12571271 if (self -> stat_atopen != NULL && self -> stat_atopen -> st_blksize > 1 ) {
12581272 return PyLong_FromLong (self -> stat_atopen -> st_blksize );
@@ -1262,11 +1276,11 @@ get_blksize(fileio *self, void *closure)
12621276}
12631277
12641278static PyGetSetDef fileio_getsetlist [] = {
1265- {"closed" , ( getter ) get_closed , NULL , "True if the file is closed" },
1266- {"closefd" , ( getter ) get_closefd , NULL ,
1279+ {"closed" , fileio_get_closed , NULL , "True if the file is closed" },
1280+ {"closefd" , fileio_get_closefd , NULL ,
12671281 "True if the file descriptor will be closed by close()." },
1268- {"mode" , ( getter ) get_mode , NULL , "String giving the file mode" },
1269- {"_blksize" , ( getter ) get_blksize , NULL , "Stat st_blksize if available" },
1282+ {"mode" , fileio_get_mode , NULL , "String giving the file mode" },
1283+ {"_blksize" , fileio_get_blksize , NULL , "Stat st_blksize if available" },
12701284 {NULL },
12711285};
12721286
0 commit comments