--- pythonrun.original.c 2015-07-03 22:01:35.065875000 +0400 +++ pythonrun.c 2015-07-03 19:15:11.459429600 +0400 @@ -1103,11 +1103,25 @@ } static int +check_fd(int fd) +{ +#if defined(HAVE_FSTAT) + struct stat buf; + if (fstat(fd, &buf) < 0 && errno == EBADF) { + return -1; + } +#endif + return 0; +} + +static int is_valid_fd(int fd) { int dummy_fd; if (fd < 0 || !_PyVerify_fd(fd)) return 0; + if (check_fd(fd) < 0) + return 0; dummy_fd = dup(fd); if (dummy_fd < 0) return 0;