@@ -86,22 +86,24 @@ typedef struct {
86
86
} fileio ;
87
87
88
88
#define PyFileIO_Check (state , op ) (PyObject_TypeCheck((op), state->PyFileIO_Type))
89
+ #define _PyFileIO_CAST (op ) _Py_CAST(fileio*, (op))
89
90
90
91
/* Forward declarations */
91
92
static PyObject * portable_lseek (fileio * self , PyObject * posobj , int whence , bool suppress_pipe_error );
92
93
93
94
int
94
95
_PyFileIO_closed (PyObject * self )
95
96
{
96
- return (( fileio * ) self )-> fd < 0 ;
97
+ return (_PyFileIO_CAST ( self )-> fd < 0 ) ;
97
98
}
98
99
99
100
/* Because this can call arbitrary code, it shouldn't be called when
100
101
the refcount is 0 (that is, not directly from tp_dealloc unless
101
102
the refcount has been temporarily re-incremented). */
102
103
static PyObject *
103
- fileio_dealloc_warn (fileio * self , PyObject * source )
104
+ fileio_dealloc_warn (PyObject * op , PyObject * source )
104
105
{
106
+ fileio * self = _PyFileIO_CAST (op );
105
107
if (self -> fd >= 0 && self -> closefd ) {
106
108
PyObject * exc = PyErr_GetRaisedException ();
107
109
if (PyErr_ResourceWarning (source , 1 , "unclosed file %R" , source )) {
@@ -171,7 +173,7 @@ _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
171
173
exc = PyErr_GetRaisedException ();
172
174
}
173
175
if (self -> finalizing ) {
174
- PyObject * r = fileio_dealloc_warn (self , (PyObject * ) self );
176
+ PyObject * r = fileio_dealloc_warn (( PyObject * ) self , (PyObject * ) self );
175
177
if (r ) {
176
178
Py_DECREF (r );
177
179
}
@@ -192,23 +194,22 @@ _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
192
194
static PyObject *
193
195
fileio_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
194
196
{
195
- fileio * self ;
196
-
197
197
assert (type != NULL && type -> tp_alloc != NULL );
198
198
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 ;
210
202
}
211
203
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 ;
212
213
return (PyObject * ) self ;
213
214
}
214
215
@@ -539,36 +540,43 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
539
540
}
540
541
541
542
static int
542
- fileio_traverse (fileio * self , visitproc visit , void * arg )
543
+ fileio_traverse (PyObject * op , visitproc visit , void * arg )
543
544
{
545
+ fileio * self = _PyFileIO_CAST (op );
544
546
Py_VISIT (Py_TYPE (self ));
545
547
Py_VISIT (self -> dict );
546
548
return 0 ;
547
549
}
548
550
549
551
static int
550
- fileio_clear (fileio * self )
552
+ fileio_clear (PyObject * op )
551
553
{
554
+ fileio * self = _PyFileIO_CAST (op );
552
555
Py_CLEAR (self -> dict );
553
556
return 0 ;
554
557
}
555
558
556
559
static void
557
- fileio_dealloc (fileio * self )
560
+ fileio_dealloc (PyObject * op )
558
561
{
559
- PyTypeObject * tp = Py_TYPE ( self );
562
+ fileio * self = _PyFileIO_CAST ( op );
560
563
self -> finalizing = 1 ;
561
- if (_PyIOBase_finalize (( PyObject * ) self ) < 0 )
564
+ if (_PyIOBase_finalize (op ) < 0 ) {
562
565
return ;
566
+ }
567
+
563
568
_PyObject_GC_UNTRACK (self );
564
569
if (self -> stat_atopen != NULL ) {
565
570
PyMem_Free (self -> stat_atopen );
566
571
self -> stat_atopen = NULL ;
567
572
}
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 );
572
580
Py_DECREF (tp );
573
581
}
574
582
@@ -1151,18 +1159,20 @@ mode_string(fileio *self)
1151
1159
}
1152
1160
1153
1161
static PyObject *
1154
- fileio_repr (fileio * self )
1162
+ fileio_repr (PyObject * op )
1155
1163
{
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 ;
1158
1166
1159
1167
if (self -> fd < 0 ) {
1160
1168
return PyUnicode_FromFormat ("<%.100s [closed]>" , type_name );
1161
1169
}
1162
1170
1171
+ PyObject * nameobj ;
1163
1172
if (PyObject_GetOptionalAttr ((PyObject * ) self , & _Py_ID (name ), & nameobj ) < 0 ) {
1164
1173
return NULL ;
1165
1174
}
1175
+ PyObject * res ;
1166
1176
if (nameobj == NULL ) {
1167
1177
res = PyUnicode_FromFormat (
1168
1178
"<%.100s fd=%d mode='%s' closefd=%s>" ,
@@ -1224,7 +1234,7 @@ static PyMethodDef fileio_methods[] = {
1224
1234
_IO_FILEIO_WRITABLE_METHODDEF
1225
1235
_IO_FILEIO_FILENO_METHODDEF
1226
1236
_IO_FILEIO_ISATTY_METHODDEF
1227
- {"_dealloc_warn" , ( PyCFunction ) fileio_dealloc_warn , METH_O , NULL },
1237
+ {"_dealloc_warn" , fileio_dealloc_warn , METH_O , NULL },
1228
1238
{"__reduce__" , _PyIOBase_cannot_pickle , METH_NOARGS },
1229
1239
{"__reduce_ex__" , _PyIOBase_cannot_pickle , METH_O },
1230
1240
{NULL , NULL } /* sentinel */
@@ -1233,26 +1243,30 @@ static PyMethodDef fileio_methods[] = {
1233
1243
/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1234
1244
1235
1245
static PyObject *
1236
- get_closed ( fileio * self , void * closure )
1246
+ fileio_get_closed ( PyObject * op , void * closure )
1237
1247
{
1248
+ fileio * self = _PyFileIO_CAST (op );
1238
1249
return PyBool_FromLong ((long )(self -> fd < 0 ));
1239
1250
}
1240
1251
1241
1252
static PyObject *
1242
- get_closefd ( fileio * self , void * closure )
1253
+ fileio_get_closefd ( PyObject * op , void * closure )
1243
1254
{
1255
+ fileio * self = _PyFileIO_CAST (op );
1244
1256
return PyBool_FromLong ((long )(self -> closefd ));
1245
1257
}
1246
1258
1247
1259
static PyObject *
1248
- get_mode ( fileio * self , void * closure )
1260
+ fileio_get_mode ( PyObject * op , void * closure )
1249
1261
{
1262
+ fileio * self = _PyFileIO_CAST (op );
1250
1263
return PyUnicode_FromString (mode_string (self ));
1251
1264
}
1252
1265
1253
1266
static PyObject *
1254
- get_blksize ( fileio * self , void * closure )
1267
+ fileio_get_blksize ( PyObject * op , void * closure )
1255
1268
{
1269
+ fileio * self = _PyFileIO_CAST (op );
1256
1270
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1257
1271
if (self -> stat_atopen != NULL && self -> stat_atopen -> st_blksize > 1 ) {
1258
1272
return PyLong_FromLong (self -> stat_atopen -> st_blksize );
@@ -1262,11 +1276,11 @@ get_blksize(fileio *self, void *closure)
1262
1276
}
1263
1277
1264
1278
static 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 ,
1267
1281
"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" },
1270
1284
{NULL },
1271
1285
};
1272
1286
0 commit comments