mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-25996: Added support of file descriptors in os.scandir() on Unix. (#502)
os.fwalk() is sped up by 2 times by using os.scandir().
This commit is contained in:
parent
0a58f72762
commit
ea720fe7e9
7 changed files with 160 additions and 44 deletions
|
@ -3313,6 +3313,35 @@ class TestScandir(unittest.TestCase):
|
|||
self.assertEqual(entry.path,
|
||||
os.fsencode(os.path.join(self.path, 'file.txt')))
|
||||
|
||||
@unittest.skipUnless(os.listdir in os.supports_fd,
|
||||
'fd support for listdir required for this test.')
|
||||
def test_fd(self):
|
||||
self.assertIn(os.scandir, os.supports_fd)
|
||||
self.create_file('file.txt')
|
||||
expected_names = ['file.txt']
|
||||
if support.can_symlink():
|
||||
os.symlink('file.txt', os.path.join(self.path, 'link'))
|
||||
expected_names.append('link')
|
||||
|
||||
fd = os.open(self.path, os.O_RDONLY)
|
||||
try:
|
||||
with os.scandir(fd) as it:
|
||||
entries = list(it)
|
||||
names = [entry.name for entry in entries]
|
||||
self.assertEqual(sorted(names), expected_names)
|
||||
self.assertEqual(names, os.listdir(fd))
|
||||
for entry in entries:
|
||||
self.assertEqual(entry.path, entry.name)
|
||||
self.assertEqual(os.fspath(entry), entry.name)
|
||||
self.assertEqual(entry.is_symlink(), entry.name == 'link')
|
||||
if os.stat in os.supports_dir_fd:
|
||||
st = os.stat(entry.name, dir_fd=fd)
|
||||
self.assertEqual(entry.stat(), st)
|
||||
st = os.stat(entry.name, dir_fd=fd, follow_symlinks=False)
|
||||
self.assertEqual(entry.stat(follow_symlinks=False), st)
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
def test_empty_path(self):
|
||||
self.assertRaises(FileNotFoundError, os.scandir, '')
|
||||
|
||||
|
@ -3328,7 +3357,7 @@ class TestScandir(unittest.TestCase):
|
|||
self.assertEqual(len(entries2), 0, entries2)
|
||||
|
||||
def test_bad_path_type(self):
|
||||
for obj in [1234, 1.234, {}, []]:
|
||||
for obj in [1.234, {}, []]:
|
||||
self.assertRaises(TypeError, os.scandir, obj)
|
||||
|
||||
def test_close(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue