Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-28041: Inconsistent behavior: Get st_nlink from os.stat() and os.scandir() #795

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions Lib/test/test_os.py
Expand Up @@ -3241,13 +3241,11 @@ def test_removed_dir(self):
self.assertFalse(entry.is_symlink())
if os.name == 'nt':
self.assertRaises(FileNotFoundError, entry.inode)
# don't fail
entry.stat()
entry.stat(follow_symlinks=False)
else:
self.assertGreater(entry.inode(), 0)
self.assertRaises(FileNotFoundError, entry.stat)
self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)
self.assertRaises(FileNotFoundError, entry.stat)
self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)


def test_removed_file(self):
entry = self.create_file_entry()
Expand All @@ -3260,13 +3258,10 @@ def test_removed_file(self):
self.assertFalse(entry.is_symlink())
if os.name == 'nt':
self.assertRaises(FileNotFoundError, entry.inode)
# don't fail
entry.stat()
entry.stat(follow_symlinks=False)
else:
self.assertGreater(entry.inode(), 0)
self.assertRaises(FileNotFoundError, entry.stat)
self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)
self.assertRaises(FileNotFoundError, entry.stat)
self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)

def test_broken_symlink(self):
if not support.can_symlink():
Expand Down Expand Up @@ -3369,6 +3364,14 @@ def test_resource_warning(self):
with self.check_no_resource_warning():
del iterator

def test_scandir_stat_match(self):
stat_basic = os.stat(__file__)
stat_scandir = os.stat(__file__)
for entry in os.scandir(os.path.dirname(os.path.abspath(__file__))):
if entry.name == os.path.basename(__file__):
stat_scandir = entry.stat()
break
self.assertEqual(stat_basic, stat_scandir)

class TestPEP519(unittest.TestCase):

Expand Down
6 changes: 1 addition & 5 deletions Modules/posixmodule.c
Expand Up @@ -11234,11 +11234,7 @@ static PyObject *
DirEntry_get_lstat(DirEntry *self)
{
if (!self->lstat) {
#ifdef MS_WINDOWS
self->lstat = _pystat_fromstructstat(&self->win32_lstat);
#else /* POSIX */
self->lstat = DirEntry_fetch_stat(self, 0);
#endif
}
Py_XINCREF(self->lstat);
return self->lstat;
Expand Down Expand Up @@ -11569,7 +11565,7 @@ DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
if (!entry->path)
goto error;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extraneous whitespace.

find_data_to_file_info(dataW, &file_info, &reparse_tag);
_Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);

Expand Down