Issue 4804.  Add a function to test the validity of file descriptors on Windows, and stop using global runtime settings to silence the warnings / assertions.
diff --git a/Modules/_fileio.c b/Modules/_fileio.c
index 2fc6de8..409abf6 100644
--- a/Modules/_fileio.c
+++ b/Modules/_fileio.c
@@ -127,7 +127,7 @@
 {
 #if defined(HAVE_FSTAT)
 	struct stat buf;
-	if (fstat(fd, &buf) < 0 && errno == EBADF) {
+	if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
 		PyObject *exc;
 		char *msg = strerror(EBADF);
 		exc = PyObject_CallFunction(PyExc_OSError, "(is)",