Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit 6072ae7

Browse files
authored
Merge pull request #93 from ahvigil/70-expose-direntry
Issue 70: expose DirEntry type -- looks good to me, thanks @ahvigil. I'm merging and I'll push a new version shortly.
2 parents ca7d5d5 + 94228ed commit 6072ae7

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

_scandir.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,8 @@ init_scandir(void)
18191819
if (PyType_Ready(&DirEntryType) < 0)
18201820
INIT_ERROR;
18211821

1822+
PyModule_AddObject(module, "DirEntry", (PyObject *)&DirEntryType);
1823+
18221824
#if PY_MAJOR_VERSION >= 3
18231825
return module;
18241826
#endif

scandir.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,17 @@ def scandir_python(path=unicode('.')):
386386

387387
if _scandir is not None:
388388
scandir_c = _scandir.scandir
389+
DirEntry_c = _scandir.DirEntry
389390

390391
if _scandir is not None:
391392
scandir = scandir_c
393+
DirEntry = DirEntry_c
392394
elif ctypes is not None:
393395
scandir = scandir_python
396+
DirEntry = Win32DirEntryPython
394397
else:
395398
scandir = scandir_generic
399+
DirEntry = GenericDirEntry
396400

397401

398402
# Linux, OS X, and BSD implementation
@@ -564,18 +568,23 @@ def scandir_python(path=unicode('.')):
564568

565569
if _scandir is not None:
566570
scandir_c = _scandir.scandir
571+
DirEntry_c = _scandir.DirEntry
567572

568573
if _scandir is not None:
569574
scandir = scandir_c
575+
DirEntry = DirEntry_c
570576
elif ctypes is not None:
571577
scandir = scandir_python
578+
DirEntry = PosixDirEntry
572579
else:
573580
scandir = scandir_generic
581+
DirEntry = GenericDirEntry
574582

575583

576584
# Some other system -- no d_type or stat information
577585
else:
578586
scandir = scandir_generic
587+
DirEntry = GenericDirEntry
579588

580589

581590
def _walk(top, topdown=True, onerror=None, followlinks=False):

test/test_scandir.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ def setUp(self):
297297
TestMixin.setUp(self)
298298

299299

300+
class TestScandirDirEntry(unittest.TestCase):
301+
def setUp(self):
302+
if not os.path.exists(TEST_PATH):
303+
setup_main()
304+
305+
def test_iter_returns_dir_entry(self):
306+
it = scandir.scandir(TEST_PATH)
307+
entry = next(it)
308+
assert isinstance(entry, scandir.DirEntry)
309+
310+
300311
if hasattr(os, 'scandir'):
301312
class TestScandirOS(TestMixin, unittest.TestCase):
302313
def setUp(self):

0 commit comments

Comments
 (0)