From 4a49a86e71232d5ee1d4e9f5482f9923717ea47e Mon Sep 17 00:00:00 2001 From: Hasan Aliyev Date: Sun, 1 Aug 2021 08:08:19 +0400 Subject: [PATCH 1/4] bpo-42053: fwalk: incorrect boolean test for non-fd arguments --- Lib/os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/os.py b/Lib/os.py index 8cc70a11e9bc89..75b64e129818d0 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -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. From 225cbb648bbecad2f9e8f173f65d8bbcb6bd7ef6 Mon Sep 17 00:00:00 2001 From: Hasan Date: Wed, 4 Aug 2021 09:58:44 +0400 Subject: [PATCH 2/4] bpo-42053: added fwalk unittest --- Lib/test/test_os.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 00e738ecf9a1c3..9681e94b8a2408 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1527,6 +1527,27 @@ 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) + + # Not isinstance of int and not has __index__ method + # Without __fspath__ method it must raise TypeError. + 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 From 33b8dc37e1221e9edc2279eea34d9131e8ec35d2 Mon Sep 17 00:00:00 2001 From: Hasan Date: Wed, 4 Aug 2021 10:02:41 +0400 Subject: [PATCH 3/4] Update test_os.py --- Lib/test/test_os.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 9681e94b8a2408..30d5f4d978a14b 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1537,8 +1537,6 @@ class CustomStrClass(CustomBaseClass): def __fspath__(self): return str(self.val) - # Not isinstance of int and not has __index__ method - # Without __fspath__ method it must raise TypeError. top = CustomBaseClass('.') with self.assertRaisesRegex(TypeError, f'open: path should be string, bytes or os.PathLike'): next(os.fwalk(top=top, follow_symlinks=True)) From dcc88bdbc9c258e48154124658d2910cef744761 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 4 Aug 2021 16:45:59 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2021-08-04-16-45-58.bpo-42053.WzOl39.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-08-04-16-45-58.bpo-42053.WzOl39.rst diff --git a/Misc/NEWS.d/next/Library/2021-08-04-16-45-58.bpo-42053.WzOl39.rst b/Misc/NEWS.d/next/Library/2021-08-04-16-45-58.bpo-42053.WzOl39.rst new file mode 100644 index 00000000000000..878825d5b6f0be --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-08-04-16-45-58.bpo-42053.WzOl39.rst @@ -0,0 +1,2 @@ +fixed incorrect boolean test for non-fd arguments in os.fwalk. +Added test with custom class and __index__ method \ No newline at end of file