Yes, despite that MSVCRT knows the type of the file descriptor because it calls GetFileType() on its creation, it doesn't check it in lseek() implementation and simply calls SetFilePointer(), which spuriously succeeds for pipes. MSDN says the following[1]:
Calling the SetFilePointer function with a handle to a non-seeking device such as a pipe or a communications device is not supported, even though the SetFilePointer function may not return an error. The behavior of the SetFilePointer function in this case is undefined.
Would a possible fix here be to check the file type within the (Windows) implementation of seek and seekable? Or is the necessary information not available by that point?
Activity
nedsociety commented on Dec 8, 2020
This happens on Windows. Consequently seek() works for these objects but they seems to be no-op. This may confuse libraries that depend on seeking.
The named pipe objects (via CreateNamedPipe -> open_osfhandle -> open()) exhibit the same behavior.
izbyshev commented on Dec 8, 2020
Yes, despite that MSVCRT knows the type of the file descriptor because it calls GetFileType() on its creation, it doesn't check it in lseek() implementation and simply calls SetFilePointer(), which spuriously succeeds for pipes. MSDN says the following[1]:
Calling the SetFilePointer function with a handle to a non-seeking device such as a pipe or a communications device is not supported, even though the SetFilePointer function may not return an error. The behavior of the SetFilePointer function in this case is undefined.
[1] https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfilepointer
PeterJCLaw commented on Jun 7, 2022
Would a possible fix here be to check the file type within the (Windows) implementation of seek and seekable? Or is the necessary information not available by that point?
vstinner commented on Jun 8, 2022
Maybe Python should get the handle from the file descriptor to call SetFilePointer() on it? Since lseek(fd) seems to silently ignores an error.
gh-86768: Raise OSError when seeking a pipe on Windows (GH-133137)
pythongh-86768: Raise OSError when seeking a pipe on Windows (pythonG…