Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
c48fe98a
Kaydet (Commit)
c48fe98a
authored
Haz 25, 2012
tarafından
Larry Hastings
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #15177: Added dir_fd parameter to os.fwalk().
üst
2a193a81
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
14 deletions
+42
-14
os.rst
Doc/library/os.rst
+5
-2
os.py
Lib/os.py
+11
-7
test_os.py
Lib/test/test_os.py
+24
-5
NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/os.rst
Dosyayı görüntüle @
c48fe98a
...
...
@@ -2211,18 +2211,21 @@ features:
os.rmdir(os.path.join(root, name))
.. function:: fwalk(top
, topdown=True, onerror=None, followlinks=Fals
e)
.. function:: fwalk(top
='.', topdown=True, onerror=None, followlinks=False, *, dir_fd=Non
e)
.. index::
single: directory; walking
single: directory; traversal
This behaves exactly like :func:`walk`, except that it yields a 4-tuple
``(dirpath, dirnames, filenames, dirfd)``.
``(dirpath, dirnames, filenames, dirfd)``
, and it supports ``dir_fd``
.
*dirpath*, *dirnames* and *filenames* are identical to :func:`walk` output,
and *dirfd* is a file descriptor referring to the directory *dirpath*.
This function always supports :ref:`paths relative to directory descriptors
<dir_fd>`.
.. note::
Since :func:`fwalk` yields file descriptors, those are only valid until
...
...
Lib/os.py
Dosyayı görüntüle @
c48fe98a
...
...
@@ -422,9 +422,9 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
__all__
.
append
(
"walk"
)
if
open
in
supports_dir
_fd
:
if
{
open
,
stat
}
<=
supports_dir_fd
and
{
listdir
,
stat
}
<=
supports
_fd
:
def
fwalk
(
top
,
topdown
=
True
,
onerror
=
None
,
followlinks
=
Fals
e
):
def
fwalk
(
top
=
"."
,
topdown
=
True
,
onerror
=
None
,
followlinks
=
False
,
*
,
dir_fd
=
Non
e
):
"""Directory tree generator.
This behaves exactly like walk(), except that it yields a 4-tuple
...
...
@@ -434,9 +434,13 @@ if open in supports_dir_fd:
`dirpath`, `dirnames` and `filenames` are identical to walk() output,
and `dirfd` is a file descriptor referring to the directory `dirpath`.
The advantage of
walkfd
() over walk() is that it's safe against symlink
The advantage of
fwalk
() over walk() is that it's safe against symlink
races (when followlinks is False).
If dir_fd is not None, it should be a file descriptor open to a directory,
and top should be relative; top will then be relative to that directory.
(dir_fd is always supported for fwalk.)
Caution:
Since fwalk() yields file descriptors, those are only valid until the
next iteration step, so you should dup() them if you want to keep them
...
...
@@ -455,11 +459,11 @@ if open in supports_dir_fd:
"""
# Note: To guard against symlink races, we use the standard
# lstat()/open()/fstat() trick.
orig_st
=
lstat
(
top
)
topfd
=
open
(
top
,
O_RDONLY
)
orig_st
=
stat
(
top
,
follow_symlinks
=
False
,
dir_fd
=
dir_fd
)
topfd
=
open
(
top
,
O_RDONLY
,
dir_fd
=
dir_fd
)
try
:
if
(
followlinks
or
(
st
.
S_ISDIR
(
orig_st
.
st_mode
)
and
path
.
samestat
(
orig_st
,
f
stat
(
topfd
)))):
path
.
samestat
(
orig_st
,
stat
(
topfd
)))):
yield
from
_fwalk
(
topfd
,
top
,
topdown
,
onerror
,
followlinks
)
finally
:
close
(
topfd
)
...
...
@@ -502,7 +506,7 @@ if open in supports_dir_fd:
onerror
(
err
)
return
try
:
if
followlinks
or
path
.
samestat
(
orig_st
,
f
stat
(
dirfd
)):
if
followlinks
or
path
.
samestat
(
orig_st
,
stat
(
dirfd
)):
dirpath
=
path
.
join
(
toppath
,
name
)
yield
from
_fwalk
(
dirfd
,
dirpath
,
topdown
,
onerror
,
followlinks
)
finally
:
...
...
Lib/test/test_os.py
Dosyayı görüntüle @
c48fe98a
...
...
@@ -741,19 +741,38 @@ class WalkTests(unittest.TestCase):
class
FwalkTests
(
WalkTests
):
"""Tests for os.fwalk()."""
def
test_compare_to_walk
(
self
):
# compare with walk() results
def
_compare_to_walk
(
self
,
walk_kwargs
,
fwalk_kwargs
):
"""
compare with walk() results.
"""
for
topdown
,
followlinks
in
itertools
.
product
((
True
,
False
),
repeat
=
2
):
args
=
support
.
TESTFN
,
topdown
,
None
,
followlinks
d
=
{
'topdown'
:
topdown
,
'followlinks'
:
followlinks
}
walk_kwargs
.
update
(
d
)
fwalk_kwargs
.
update
(
d
)
expected
=
{}
for
root
,
dirs
,
files
in
os
.
walk
(
*
args
):
for
root
,
dirs
,
files
in
os
.
walk
(
*
*
walk_kw
args
):
expected
[
root
]
=
(
set
(
dirs
),
set
(
files
))
for
root
,
dirs
,
files
,
rootfd
in
os
.
fwalk
(
*
args
):
for
root
,
dirs
,
files
,
rootfd
in
os
.
fwalk
(
*
*
fwalk_kw
args
):
self
.
assertIn
(
root
,
expected
)
self
.
assertEqual
(
expected
[
root
],
(
set
(
dirs
),
set
(
files
)))
def
test_compare_to_walk
(
self
):
kwargs
=
{
'top'
:
support
.
TESTFN
}
self
.
_compare_to_walk
(
kwargs
,
kwargs
)
def
test_dir_fd
(
self
):
try
:
fd
=
os
.
open
(
"."
,
os
.
O_RDONLY
)
walk_kwargs
=
{
'top'
:
support
.
TESTFN
}
fwalk_kwargs
=
walk_kwargs
.
copy
()
fwalk_kwargs
[
'dir_fd'
]
=
fd
self
.
_compare_to_walk
(
walk_kwargs
,
fwalk_kwargs
)
finally
:
os
.
close
(
fd
)
def
test_yields_correct_dir_fd
(
self
):
# check returned file descriptors
for
topdown
,
followlinks
in
itertools
.
product
((
True
,
False
),
repeat
=
2
):
args
=
support
.
TESTFN
,
topdown
,
None
,
followlinks
...
...
Misc/NEWS
Dosyayı görüntüle @
c48fe98a
...
...
@@ -59,6 +59,8 @@ Core and Builtins
Library
-------
-
Issue
#
15177
:
Added
dir_fd
parameter
to
os
.
fwalk
().
-
Issue
#
15176
:
Clarified
behavior
,
documentation
,
and
implementation
of
os
.
listdir
().
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment