Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a32c4d0

Browse files
committed
Issue #27038: Expose DirEntry as os.DirEntry.
Thanks to Jelle Zijlstra for the code portion of the patch.
1 parent 620d9c7 commit a32c4d0

4 files changed

Lines changed: 31 additions & 24 deletions

File tree

Doc/library/os.rst

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,25 +1920,26 @@ features:
19201920

19211921
.. function:: scandir(path='.')
19221922

1923-
Return an iterator of :class:`DirEntry` objects corresponding to the entries
1924-
in the directory given by *path*. The entries are yielded in arbitrary
1925-
order, and the special entries ``'.'`` and ``'..'`` are not included.
1923+
Return an iterator of :class:`os.DirEntry` objects corresponding to the
1924+
entries in the directory given by *path*. The entries are yielded in
1925+
arbitrary order, and the special entries ``'.'`` and ``'..'`` are not
1926+
included.
19261927

19271928
Using :func:`scandir` instead of :func:`listdir` can significantly
19281929
increase the performance of code that also needs file type or file
1929-
attribute information, because :class:`DirEntry` objects expose this
1930+
attribute information, because :class:`os.DirEntry` objects expose this
19301931
information if the operating system provides it when scanning a directory.
1931-
All :class:`DirEntry` methods may perform a system call, but
1932-
:func:`~DirEntry.is_dir` and :func:`~DirEntry.is_file` usually only
1933-
require a system call for symbolic links; :func:`DirEntry.stat`
1932+
All :class:`os.DirEntry` methods may perform a system call, but
1933+
:func:`~os.DirEntry.is_dir` and :func:`~os.DirEntry.is_file` usually only
1934+
require a system call for symbolic links; :func:`os.DirEntry.stat`
19341935
always requires a system call on Unix but only requires one for
19351936
symbolic links on Windows.
19361937

19371938
On Unix, *path* can be of type :class:`str` or :class:`bytes` (use
19381939
:func:`~os.fsencode` and :func:`~os.fsdecode` to encode and decode
19391940
:class:`bytes` paths). On Windows, *path* must be of type :class:`str`.
1940-
On both sytems, the type of the :attr:`~DirEntry.name` and
1941-
:attr:`~DirEntry.path` attributes of each :class:`DirEntry` will be of
1941+
On both sytems, the type of the :attr:`~os.DirEntry.name` and
1942+
:attr:`~os.DirEntry.path` attributes of each :class:`os.DirEntry` will be of
19421943
the same type as *path*.
19431944

19441945
The :func:`scandir` iterator supports the :term:`context manager` protocol
@@ -1993,22 +1994,22 @@ features:
19931994

19941995
:func:`scandir` will provide as much of this information as possible without
19951996
making additional system calls. When a ``stat()`` or ``lstat()`` system call
1996-
is made, the ``DirEntry`` object will cache the result.
1997+
is made, the ``os.DirEntry`` object will cache the result.
19971998

1998-
``DirEntry`` instances are not intended to be stored in long-lived data
1999+
``os.DirEntry`` instances are not intended to be stored in long-lived data
19992000
structures; if you know the file metadata has changed or if a long time has
20002001
elapsed since calling :func:`scandir`, call ``os.stat(entry.path)`` to fetch
20012002
up-to-date information.
20022003

2003-
Because the ``DirEntry`` methods can make operating system calls, they may
2004+
Because the ``os.DirEntry`` methods can make operating system calls, they may
20042005
also raise :exc:`OSError`. If you need very fine-grained
20052006
control over errors, you can catch :exc:`OSError` when calling one of the
2006-
``DirEntry`` methods and handle as appropriate.
2007+
``os.DirEntry`` methods and handle as appropriate.
20072008

2008-
To be directly usable as a :term:`path-like object`, ``DirEntry`` implements
2009-
the :class:`os.PathLike` interface.
2009+
To be directly usable as a :term:`path-like object`, ``os.DirEntry``
2010+
implements the :class:`os.PathLike` interface.
20102011

2011-
Attributes and methods on a ``DirEntry`` instance are as follows:
2012+
Attributes and methods on a ``os.DirEntry`` instance are as follows:
20122013

20132014
.. attribute:: name
20142015

@@ -2034,8 +2035,9 @@ features:
20342035

20352036
Return the inode number of the entry.
20362037

2037-
The result is cached on the ``DirEntry`` object. Use ``os.stat(entry.path,
2038-
follow_symlinks=False).st_ino`` to fetch up-to-date information.
2038+
The result is cached on the ``os.DirEntry`` object. Use
2039+
``os.stat(entry.path, follow_symlinks=False).st_ino`` to fetch up-to-date
2040+
information.
20392041

20402042
On the first, uncached call, a system call is required on Windows but
20412043
not on Unix.
@@ -2050,7 +2052,7 @@ features:
20502052
is a directory (without following symlinks); return ``False`` if the
20512053
entry is any other kind of file or if it doesn't exist anymore.
20522054

2053-
The result is cached on the ``DirEntry`` object, with a separate cache
2055+
The result is cached on the ``os.DirEntry`` object, with a separate cache
20542056
for *follow_symlinks* ``True`` and ``False``. Call :func:`os.stat` along
20552057
with :func:`stat.S_ISDIR` to fetch up-to-date information.
20562058

@@ -2074,16 +2076,16 @@ features:
20742076
is a file (without following symlinks); return ``False`` if the entry is
20752077
a directory or other non-file entry, or if it doesn't exist anymore.
20762078

2077-
The result is cached on the ``DirEntry`` object. Caching, system calls
2078-
made, and exceptions raised are as per :func:`~DirEntry.is_dir`.
2079+
The result is cached on the ``os.DirEntry`` object. Caching, system calls
2080+
made, and exceptions raised are as per :func:`~os.DirEntry.is_dir`.
20792081

20802082
.. method:: is_symlink()
20812083

20822084
Return ``True`` if this entry is a symbolic link (even if broken);
20832085
return ``False`` if the entry points to a directory or any kind of file,
20842086
or if it doesn't exist anymore.
20852087

2086-
The result is cached on the ``DirEntry`` object. Call
2088+
The result is cached on the ``os.DirEntry`` object. Call
20872089
:func:`os.path.islink` to fetch up-to-date information.
20882090

20892091
On the first, uncached call, no system call is required in most cases.
@@ -2108,12 +2110,12 @@ features:
21082110
:class:`stat_result` are always set to zero. Call :func:`os.stat` to
21092111
get these attributes.
21102112

2111-
The result is cached on the ``DirEntry`` object, with a separate cache
2113+
The result is cached on the ``os.DirEntry`` object, with a separate cache
21122114
for *follow_symlinks* ``True`` and ``False``. Call :func:`os.stat` to
21132115
fetch up-to-date information.
21142116

21152117
Note that there is a nice correspondence between several attributes
2116-
and methods of ``DirEntry`` and of :class:`pathlib.Path`. In
2118+
and methods of ``os.DirEntry`` and of :class:`pathlib.Path`. In
21172119
particular, the ``name`` attribute has the same
21182120
meaning, as do the ``is_dir()``, ``is_file()``, ``is_symlink()``
21192121
and ``stat()`` methods.

Lib/test/test_os.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,7 @@ def assert_stat_equal(self, stat1, stat2, skip_fields):
28542854
self.assertEqual(stat1, stat2)
28552855

28562856
def check_entry(self, entry, name, is_dir, is_file, is_symlink):
2857+
self.assertIsInstance(entry, os.DirEntry)
28572858
self.assertEqual(entry.name, name)
28582859
self.assertEqual(entry.path, os.path.join(self.path, name))
28592860
self.assertEqual(entry.inode(),

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 3
1010
Library
1111
-------
1212

13+
- Issue #27038: Expose the DirEntry type as os.DirEntry. Code patch by
14+
Jelle Zijlstra.
15+
1316
- Issue #27186: Update os.fspath()/PyOS_FSPath() to check the return value of
1417
__fspath__() to be either str or bytes.
1518

Modules/posixmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13320,6 +13320,7 @@ INITFUNC(void)
1332013320
Py_DECREF(unicode);
1332113321
}
1332213322
PyModule_AddObject(m, "_have_functions", list);
13323+
PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
1332313324

1332413325
initialized = 1;
1332513326

0 commit comments

Comments
 (0)