In Windows, GetFileType((HANDLE)_get_osfhandle(fd)) is several times faster than close(dup(fd)). For example:
#if defined(MS_WINDOWS)
int type;
_Py_BEGIN_SUPPRESS_IPH
type = GetFileType((HANDLE)_get_osfhandle(fd));
_Py_END_SUPPRESS_IPH
return type != FILE_TYPE_UNKNOWN;
#endif
If the C file descriptor isn't assigned and flagged FOPEN, then _get_osfhandle() returns (intptr_t)-1. This is the pseudohandle value for the current process, so GetFileType() will fail with an invalid handle error, as expected. |