Skip to content

gh-86768: implement os.lseek with SetFilePointer on Windows #133137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2577,6 +2577,14 @@ def test_ftruncate(self):
def test_lseek(self):
self.check(os.lseek, 0, 0)

@unittest.skipUnless(hasattr(os, 'lseek'), 'test needs os.lseek()')
@unittest.skipUnless(hasattr(os, 'pipe'), "need os.pipe()")
def test_lseek_on_pipe(self):
rfd, wfd = os.pipe()
self.addCleanup(os.close, rfd)
self.addCleanup(os.close, wfd)
self.assertRaises(OSError, os.lseek, rfd, 123, os.SEEK_END)

@unittest.skipUnless(hasattr(os, 'read'), 'test needs os.read()')
def test_read(self):
self.check(os.read, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:exc:`OSError` will be raised if call :func:`os.lseek` with non seekable
object on Windows.
43 changes: 40 additions & 3 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
# if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
# define HAVE_SYMLINK
# endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_SYSTEM */
extern int winerror_to_errno(int);
#endif


Expand Down Expand Up @@ -11406,7 +11407,7 @@ static Py_off_t
os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
/*[clinic end generated code: output=971e1efb6b30bd2f input=f096e754c5367504]*/
{
Py_off_t result;
Py_off_t result = 0;

#ifdef SEEK_SET
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
Expand All @@ -11420,14 +11421,50 @@ os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
result = _lseeki64(fd, position, how);
switch (how) {
case SEEK_SET:
how = FILE_BEGIN;
break;
case SEEK_CUR:
how = FILE_CURRENT;
break;
case SEEK_END:
how = FILE_END;
break;
}
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
result = -1;
}
if (result >= 0) {
if (GetFileType(h) != FILE_TYPE_DISK) {
// Only file is seekable
errno = ESPIPE;
result = -1;
}
}
if (result >= 0) {
LARGE_INTEGER distance, newdistance;
distance.QuadPart = position;
if (SetFilePointerEx(h, distance, &newdistance, how)) {
result = newdistance.QuadPart;
} else {
result = -1;
}
}
#else
result = lseek(fd, position, how);
#endif
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
if (result < 0)
if (result < 0) {
#ifdef MS_WINDOWS
if (errno == 0) {
errno = winerror_to_errno(GetLastError());
}
#endif
posix_error();
}

return result;
}
Expand Down
Loading