Skip to content

Commit

Permalink
pythongh-117968: Make the test for closed file more safe in the C API…
Browse files Browse the repository at this point in the history
… tests (pythonGH-118230)

The behavior of fileno() after fclose() is undefined, but it is the only
practical way to check whether the file was closed.
Only test this on the known platforms (Linux, Windows, macOS), where we
already tested that it works.
  • Loading branch information
serhiy-storchaka authored Apr 25, 2024
1 parent 4b10e20 commit 546cbcf
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Modules/_testcapi/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ run_fileexflags(PyObject *mod, PyObject *pos_args)

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

#if !defined(__wasi__)
/* The behavior of fileno() after fclose() is undefined. */
#if defined(__linux__) || defined(MS_WINDOWS) || defined(__APPLE__)
/* The behavior of fileno() after fclose() is undefined, but it is
* the only practical way to check whether the file was closed.
* Only test this on the known platforms. */
if (closeit && result && fileno(fp) >= 0) {
PyErr_SetString(PyExc_AssertionError, "File was not closed after excution");
Py_DECREF(result);
Expand Down

0 comments on commit 546cbcf

Please sign in to comment.