Skip to content

Commit 4cba722

Browse files
committed
gh-118422: Fix run_fileexflags() test
Don't test undefined fileno() behavior on a closed file, but use fstat() as a reliable test if the file was closed or not.
1 parent 11f8348 commit 4cba722

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Modules/_testcapi/run.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,19 @@ run_fileexflags(PyObject *mod, PyObject *pos_args)
7171
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
7272
return NULL;
7373
}
74+
int fd = fileno(fp);
7475

7576
result = PyRun_FileExFlags(fp, filename, start, globals, locals, closeit, pflags);
7677

77-
#if defined(__linux__) || defined(MS_WINDOWS) || defined(__APPLE__)
78-
/* The behavior of fileno() after fclose() is undefined, but it is
79-
* the only practical way to check whether the file was closed.
80-
* Only test this on the known platforms. */
81-
if (closeit && result && fileno(fp) >= 0) {
78+
struct stat st;
79+
if (closeit && result && fstat(fd, &st) == 0) {
8280
PyErr_SetString(PyExc_AssertionError, "File was not closed after excution");
8381
Py_DECREF(result);
8482
fclose(fp);
8583
return NULL;
8684
}
87-
#endif
88-
if (!closeit && fileno(fp) < 0) {
85+
86+
if (!closeit && fstat(fd, &st) != 0) {
8987
PyErr_SetString(PyExc_AssertionError, "Bad file descriptor after excution");
9088
Py_XDECREF(result);
9189
return NULL;

0 commit comments

Comments
 (0)