Skip to content

bpo-42053: fixed fwalk incorrect boolean test for non-fd arguments #27524

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=
dirs.remove('CVS') # don't visit CVS directories
"""
sys.audit("os.fwalk", top, topdown, onerror, follow_symlinks, dir_fd)
if not isinstance(top, int) or not hasattr(top, '__index__'):
if not isinstance(top, int) and not hasattr(top, '__index__'):
top = fspath(top)
# Note: To guard against symlink races, we use the standard
# lstat()/open()/fstat() trick.
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,25 @@ def test_fd_leak(self):
self.addCleanup(os.close, newfd)
self.assertEqual(newfd, minfd)

def test_fwalk(self):
class CustomBaseClass():
def __init__(self, val):
self.val = val
def __index__(self):
return os.open(self.val, os.O_RDONLY)
class CustomStrClass(CustomBaseClass):
def __fspath__(self):
return str(self.val)

top = CustomBaseClass('.')
with self.assertRaisesRegex(TypeError, f'open: path should be string, bytes or os.PathLike'):
next(os.fwalk(top=top, follow_symlinks=True))

top = CustomStrClass('.')
root, dirs, files, rootfd = next(os.fwalk(top=top, follow_symlinks=True))
self.assertIsInstance(root, CustomStrClass)
self.assertNotIsInstance(root, str)

# fwalk() keeps file descriptors open
test_walk_many_open_files = None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fixed incorrect boolean test for non-fd arguments in os.fwalk.
Added test with custom class and __index__ method