Skip to content

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

Closed as not planned
Closed as not planned
@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
if dir_fd is None:
return os.path.lexists(pathname)
try:
os.lstat(pathname, dir_fd=dir_fd)
except (OSError, ValueError):
return False
else:
return True
def _isdir(pathname, dir_fd):
# Same as os.path.isdir(), but with dir_fd
if dir_fd is None:
return os.path.isdir(pathname)
try:
st = os.stat(pathname, dir_fd=dir_fd)
except (OSError, ValueError):
return False
else:
return stat.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

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions