Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
aisk wants to merge7 commits intopython:main
base:main
Choose a base branch
Loading
fromaisk:win-lseek

Conversation

aisk
Copy link
Contributor

@aiskaisk commentedApr 29, 2025
edited by bedevere-appbot
Loading

This change will introduce a performance regression:

import osimport timeitf = 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.pyRunning Release|x64 interpreter...93.8409629999951

After the change:

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

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

@aiskaisk marked this pull request as ready for reviewApril 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;
}
}

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 checkingGetFileType()? It looks much simpler.

Copy link
ContributorAuthor

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'sfd to Windows'shandle internally, which we've already called, so useSetFilePointerEx with thehandle 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());
}

Choose a reason for hiding this comment

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

Why not usePyErr_SetFromWindowsErr(0)?

Copy link
ContributorAuthor

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 toGetLastError instead oferrno, so we should check it. But after changed to call_lseeki64 directly, there is no need for this line of code.

@aiskaisk changed the titlegh-86768: implement os.lseek with SetFilePointer on Windowsgh-86768: check if fd is seekable in os.lseek on WindowsMay 10, 2025
Copy link
Member

@serhiy-storchakaserhiy-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 setresult 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);

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-storchakaserhiy-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. 👍

aisk reacted with heart emoji
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@aisk@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp