Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-86768: check if fd is seekable in os.lseek 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 7 commits into
base: main
Choose a base branch
Loading
from

Conversation

aisk
Copy link
Contributor

@aisk aisk commented Apr 29, 2025

This change will introduce a performance regression:

import os
import timeit
f = open("a.py")
print(timeit.timeit("os.lseek(f.fileno(), 1, os.SEEK_CUR)", number=100000000, globals=globals()))
f.close()

Before the change:

PS C:\Users\xxxxx\Source\cpython> .\python.bat a.py
Running Release|x64 interpreter...
93.8409629999951

After the change:

PS C:\Users\xxxxx\Source\cpython> .\python.bat a.py
Running Release|x64 interpreter...
123.18093929998577

However I think it's acceptable because we added a check in the implementation and os.lseek usually won't been called too many times in the real world.

@aisk aisk marked this pull request as ready for review April 29, 2025 16:01
Comment on lines 11446 to 11454
if (result >= 0) {
LARGE_INTEGER distance, newdistance;
distance.QuadPart = position;
if (SetFilePointerEx(h, distance, &newdistance, how)) {
result = newdistance.QuadPart;
} else {
result = -1;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply use _lseeki64() after checking GetFileType()? It looks much simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought Windows's CRT will call _get_osfhandle to convert the POSIX's fd to Windows's handle internally, which we've already called, so use SetFilePointerEx with the handle directly will gain some performance benefit. But I run the small benchmark script which mentioned at the top of this PR, and didn't see any noticeable performance regret, so updated to use _lseeki64 directly.

Comment on lines 11462 to 11464
if (errno == 0) {
errno = winerror_to_errno(GetLastError());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use PyErr_SetFromWindowsErr(0)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetFilePointerEx will set the error to GetLastError instead of errno, so we should check it. But after changed to call _lseeki64 directly, there is no need for this line of code.

@aisk aisk changed the title gh-86768: implement os.lseek with SetFilePointer on Windows gh-86768: check if fd is seekable in os.lseek on Windows May 10, 2025
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be slightly simpler if set result to -1 initially.

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer used.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.