mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-108638: Fix stat.filemode() when _stat is missing (#108639)
Change the pure Python implementation of stat.filemode() for unknown file type: use "?", as done by the _stat.filemode(). test_stat skips TestFilemodeCStat if the _stat extension is missing.
This commit is contained in:
parent
7659128b9d
commit
b62a76043e
2 changed files with 13 additions and 3 deletions
10
Lib/stat.py
10
Lib/stat.py
|
@ -126,6 +126,8 @@ SF_SNAPSHOT = 0x00200000 # file is a snapshot file
|
||||||
|
|
||||||
|
|
||||||
_filemode_table = (
|
_filemode_table = (
|
||||||
|
# File type chars according to:
|
||||||
|
# http://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h
|
||||||
((S_IFLNK, "l"),
|
((S_IFLNK, "l"),
|
||||||
(S_IFSOCK, "s"), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR
|
(S_IFSOCK, "s"), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR
|
||||||
(S_IFREG, "-"),
|
(S_IFREG, "-"),
|
||||||
|
@ -156,13 +158,17 @@ _filemode_table = (
|
||||||
def filemode(mode):
|
def filemode(mode):
|
||||||
"""Convert a file's mode to a string of the form '-rwxrwxrwx'."""
|
"""Convert a file's mode to a string of the form '-rwxrwxrwx'."""
|
||||||
perm = []
|
perm = []
|
||||||
for table in _filemode_table:
|
for index, table in enumerate(_filemode_table):
|
||||||
for bit, char in table:
|
for bit, char in table:
|
||||||
if mode & bit == bit:
|
if mode & bit == bit:
|
||||||
perm.append(char)
|
perm.append(char)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
perm.append("-")
|
if index == 0:
|
||||||
|
# Unknown filetype
|
||||||
|
perm.append("?")
|
||||||
|
else:
|
||||||
|
perm.append("-")
|
||||||
return "".join(perm)
|
return "".join(perm)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -122,8 +122,11 @@ class TestFilemode:
|
||||||
st_mode, modestr = self.get_mode()
|
st_mode, modestr = self.get_mode()
|
||||||
self.assertEqual(modestr, '-rwx------')
|
self.assertEqual(modestr, '-rwx------')
|
||||||
self.assertS_IS("REG", st_mode)
|
self.assertS_IS("REG", st_mode)
|
||||||
self.assertEqual(self.statmod.S_IMODE(st_mode),
|
imode = self.statmod.S_IMODE(st_mode)
|
||||||
|
self.assertEqual(imode,
|
||||||
self.statmod.S_IRWXU)
|
self.statmod.S_IRWXU)
|
||||||
|
self.assertEqual(self.statmod.filemode(imode),
|
||||||
|
'?rwx------')
|
||||||
|
|
||||||
os.chmod(TESTFN, 0o070)
|
os.chmod(TESTFN, 0o070)
|
||||||
st_mode, modestr = self.get_mode()
|
st_mode, modestr = self.get_mode()
|
||||||
|
@ -238,6 +241,7 @@ class TestFilemode:
|
||||||
self.assertEqual(value, modvalue, key)
|
self.assertEqual(value, modvalue, key)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipIf(c_stat is None, 'need _stat extension')
|
||||||
class TestFilemodeCStat(TestFilemode, unittest.TestCase):
|
class TestFilemodeCStat(TestFilemode, unittest.TestCase):
|
||||||
statmod = c_stat
|
statmod = c_stat
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue