Skip to content

Add dir_fd to os.path.lexists() & os.path.isdir()#117967

@nineteendo

Description

@nineteendo

Feature or enhancement

Proposal:

glob currently needs its own implementation of os.path.lexists() & os.path.isdir() to support dir_fd:

cpython/Lib/glob.py

Lines 201 to 222 in f74e512

def_lexists(pathname, dir_fd):
# Same as os.path.lexists(), but with dir_fd
ifdir_fdisNone:
returnos.path.lexists(pathname)
try:
os.lstat(pathname, dir_fd=dir_fd)
except (OSError, ValueError):
returnFalse
else:
returnTrue
def_isdir(pathname, dir_fd):
# Same as os.path.isdir(), but with dir_fd
ifdir_fdisNone:
returnos.path.isdir(pathname)
try:
st=os.stat(pathname, dir_fd=dir_fd)
except (OSError, ValueError):
returnFalse
else:
returnstat.S_ISDIR(st.st_mode)

We could refactor this by adding dir_fd to os.path.lexists() & os.path.isdir():

-def lexists(path):+def lexists(path, *, dir_fd: int | None = None): """Test whether a path exists. Returns True for broken symbolic links""" try: - os.lstat(path)+ os.lstat(path, dir_fd=dir_fd) except (OSError, ValueError): return False return True
-def isdir(s):+def isdir(s, *, dir_fd: int | None = None): """Return true if the pathname refers to an existing directory.""" try: - st = os.stat(s)+ st = os.stat(s, dir_fd=dir_fd) except (OSError, ValueError): return False return stat.S_ISDIR(st.st_mode)

Note: nt._path_isdir() (& nt._path_lexists() when #117842 lands) need to raise an error for this.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions