Skip to content

os.lstat() supports dir_fd but is not in os.supports_dir_fd #134993

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

Closed
tormol opened this issue Jun 1, 2025 · 3 comments
Closed

os.lstat() supports dir_fd but is not in os.supports_dir_fd #134993

tormol opened this issue Jun 1, 2025 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tormol
Copy link

tormol commented Jun 1, 2025

Bug report

Bug description:

I had written a script using os.fwalk() and os.lstat(), which was working.
Then I added an if os.lstat in os.supports_dir_fd check and a fallback implementation and observed the fallback path being taken:

import os, sys
from datetime import datetime

oldest = datetime.max
newest = datetime.min
oldest_file = None
newest_file = None

if os.lstat in os.supports_dir_fd:
    for dir, _, files, dirfd in os.fwalk(sys.argv[1]):
        for file in files:
            mtime = os.lstat(file, dir_fd=dirfd).st_mtime
            date = datetime.fromtimestamp(mtime)
            if date < oldest:
                oldest = date
                oldest_file = os.path.join(dir, file)
            if date > newest:
                newest = date
                newest_file = os.path.join(dir, file)
else:
    print('not using dir_fd')
    # for dir, _, files in os.walk(sys.argv[1], followlinks=False): ...

print('Newest:', newest.strftime('%Y-%m-%d %H-%M-%S'), newest_file)
print('Oldest:', oldest.strftime('%Y-%m-%d %H-%M-%S'), oldest_file)

The workaround is to check for os.stat instead (and use os.stat(..., follow_symlinks=False) for consistency).

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

@tormol tormol added the type-bug An unexpected behavior, bug, or error label Jun 1, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label Jun 1, 2025
@zangjiucheng
Copy link
Contributor

zangjiucheng commented Jun 4, 2025

Hi there, I tested this code and reproduce the case as you described.

I noticed the following code in os.py; it includes all the attributes included in supports_dir_fd.

_add("HAVE_FSTATAT", "stat")

It included _add("HAVE_FSTATAT","stat") but not included _add("HAVE_LSTAT","lstat"). I am uncertain whether this was intentionally designed or, in the alternative, we should incorporate it.

Reference:
https://linux.die.net/man/2/lstat

@vstinner
Copy link
Member

vstinner commented Jun 5, 2025

I proposed #135188 to fix the issue.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 6, 2025
(cherry picked from commit e004cf8)

Co-authored-by: Victor Stinner <vstinner@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 6, 2025
(cherry picked from commit e004cf8)

Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner
Copy link
Member

vstinner commented Jun 6, 2025

Fixed. Thanks for your report.

In the meanwhile, don't rely on os.supports_dir_fd but makes the assumption that os.lstat() supports dir_fd.

@vstinner vstinner closed this as completed Jun 6, 2025
vstinner added a commit that referenced this issue Jun 6, 2025
…135206)

gh-134993: Add os.lstat() to os.supports_dir_fd (GH-135188)
(cherry picked from commit e004cf8)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this issue Jun 6, 2025
…135205)

gh-134993: Add os.lstat() to os.supports_dir_fd (GH-135188)
(cherry picked from commit e004cf8)

Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants