Commit c77d9f38 authored by Christian Heimes's avatar Christian Heimes

Issue #11016: Add C implementation of the stat module as _stat

parent 6ce8d17d
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
os.lstat() and os.fstat(). os.lstat() and os.fstat().
.. sectionauthor:: Skip Montanaro <skip@automatrix.com> .. sectionauthor:: Skip Montanaro <skip@automatrix.com>
**Source code:** :source:`Lib/stat.py` **Source code:** :source:`Modules/_stat.c`
:source:`Lib/stat.py`
-------------- --------------
...@@ -15,6 +16,9 @@ results of :func:`os.stat`, :func:`os.fstat` and :func:`os.lstat` (if they ...@@ -15,6 +16,9 @@ results of :func:`os.stat`, :func:`os.fstat` and :func:`os.lstat` (if they
exist). For complete details about the :c:func:`stat`, :c:func:`fstat` and exist). For complete details about the :c:func:`stat`, :c:func:`fstat` and
:c:func:`lstat` calls, consult the documentation for your system. :c:func:`lstat` calls, consult the documentation for your system.
.. versionchanged:: 3.4
The stat module is backed by a C implementation.
The :mod:`stat` module defines the following functions to test for specific file The :mod:`stat` module defines the following functions to test for specific file
types: types:
...@@ -53,6 +57,24 @@ types: ...@@ -53,6 +57,24 @@ types:
Return non-zero if the mode is from a socket. Return non-zero if the mode is from a socket.
.. function:: S_ISDOOR(mode)
Return non-zero if the mode is from a door.
.. versionadded:: 3.4
.. function:: S_ISPORT(mode)
Return non-zero if the mode is from an event port.
.. versionadded:: 3.4
.. function:: S_ISWHT(mode)
Return non-zero if the mode is from a whiteout.
.. versionadded:: 3.4
Two additional functions are defined for more general manipulation of the file's Two additional functions are defined for more general manipulation of the file's
mode: mode:
...@@ -113,6 +135,10 @@ readable string: ...@@ -113,6 +135,10 @@ readable string:
.. versionadded:: 3.3 .. versionadded:: 3.3
.. versionchanged:: 3.4
The function supports :data:`S_IFDOOR`, :data:`S_IFPORT` and
:data:`S_IFWHT`.
All the variables below are simply symbolic indexes into the 10-tuple returned All the variables below are simply symbolic indexes into the 10-tuple returned
by :func:`os.stat`, :func:`os.fstat` or :func:`os.lstat`. by :func:`os.stat`, :func:`os.fstat` or :func:`os.lstat`.
...@@ -210,6 +236,29 @@ Use of the functions above is more portable than use of the first set of flags: ...@@ -210,6 +236,29 @@ Use of the functions above is more portable than use of the first set of flags:
FIFO. FIFO.
.. data:: S_IFDOOR
Door.
.. versionadded:: 3.4
.. data:: S_IFPORT
Event port.
.. versionadded:: 3.4
.. data:: S_IFWHT
Whiteout.
.. versionadded:: 3.4
.. note::
:data:`S_IFDOOR`, :data:`S_IFPORT` or :data:`S_IFWHT` are defined as
0 when the platform does not have support for the file types.
The following flags can also be used in the *mode* argument of :func:`os.chmod`: The following flags can also be used in the *mode* argument of :func:`os.chmod`:
.. data:: S_ISUID .. data:: S_ISUID
......
...@@ -182,7 +182,6 @@ functools ...@@ -182,7 +182,6 @@ functools
New :func:`functools.singledispatch` decorator: see the :pep:`443`. New :func:`functools.singledispatch` decorator: see the :pep:`443`.
smtplib smtplib
------- -------
...@@ -213,6 +212,12 @@ wave ...@@ -213,6 +212,12 @@ wave
The :meth:`~wave.getparams` method now returns a namedtuple rather than a The :meth:`~wave.getparams` method now returns a namedtuple rather than a
plain tuple. (Contributed by Claudiu Popa in :issue:`17487`.) plain tuple. (Contributed by Claudiu Popa in :issue:`17487`.)
stat
---
The stat module is now backed by a C implementation in :mod:`_stat`. A C
implementation is required as most of the values aren't standardized and
platform-dependent. (Contributed by Christian Heimes in :issue:`11016`.)
Optimizations Optimizations
============= =============
......
...@@ -147,3 +147,9 @@ def filemode(mode): ...@@ -147,3 +147,9 @@ def filemode(mode):
else: else:
perm.append("-") perm.append("-")
return "".join(perm) return "".join(perm)
# If available, use C implementation
try:
from _stat import *
except ModuleNotFoundError:
pass
import unittest import unittest
import os import os
from test.support import TESTFN, run_unittest, import_fresh_module from test.support import TESTFN, run_unittest, import_fresh_module
import stat
c_stat = import_fresh_module('stat', fresh=['_stat'])
py_stat = import_fresh_module('stat', blocked=['_stat'])
class TestFilemode(unittest.TestCase): class TestFilemode(unittest.TestCase):
statmod = None
file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK', file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK',
'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN', 'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN',
'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'} 'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'}
...@@ -60,17 +64,17 @@ class TestFilemode(unittest.TestCase): ...@@ -60,17 +64,17 @@ class TestFilemode(unittest.TestCase):
def get_mode(self, fname=TESTFN): def get_mode(self, fname=TESTFN):
st_mode = os.lstat(fname).st_mode st_mode = os.lstat(fname).st_mode
modestr = stat.filemode(st_mode) modestr = self.statmod.filemode(st_mode)
return st_mode, modestr return st_mode, modestr
def assertS_IS(self, name, mode): def assertS_IS(self, name, mode):
# test format, lstrip is for S_IFIFO # test format, lstrip is for S_IFIFO
fmt = getattr(stat, "S_IF" + name.lstrip("F")) fmt = getattr(self.statmod, "S_IF" + name.lstrip("F"))
self.assertEqual(stat.S_IFMT(mode), fmt) self.assertEqual(self.statmod.S_IFMT(mode), fmt)
# test that just one function returns true # test that just one function returns true
testname = "S_IS" + name testname = "S_IS" + name
for funcname in self.format_funcs: for funcname in self.format_funcs:
func = getattr(stat, funcname, None) func = getattr(self.statmod, funcname, None)
if func is None: if func is None:
if funcname == testname: if funcname == testname:
raise ValueError(funcname) raise ValueError(funcname)
...@@ -88,35 +92,35 @@ class TestFilemode(unittest.TestCase): ...@@ -88,35 +92,35 @@ class TestFilemode(unittest.TestCase):
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(stat.S_IMODE(st_mode), self.assertEqual(self.statmod.S_IMODE(st_mode),
stat.S_IRWXU) self.statmod.S_IRWXU)
os.chmod(TESTFN, 0o070) os.chmod(TESTFN, 0o070)
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(stat.S_IMODE(st_mode), self.assertEqual(self.statmod.S_IMODE(st_mode),
stat.S_IRWXG) self.statmod.S_IRWXG)
os.chmod(TESTFN, 0o007) os.chmod(TESTFN, 0o007)
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(stat.S_IMODE(st_mode), self.assertEqual(self.statmod.S_IMODE(st_mode),
stat.S_IRWXO) self.statmod.S_IRWXO)
os.chmod(TESTFN, 0o444) os.chmod(TESTFN, 0o444)
st_mode, modestr = self.get_mode() st_mode, modestr = self.get_mode()
self.assertS_IS("REG", st_mode) self.assertS_IS("REG", st_mode)
self.assertEqual(modestr, '-r--r--r--') self.assertEqual(modestr, '-r--r--r--')
self.assertEqual(stat.S_IMODE(st_mode), 0o444) self.assertEqual(self.statmod.S_IMODE(st_mode), 0o444)
else: else:
os.chmod(TESTFN, 0o700) os.chmod(TESTFN, 0o700)
st_mode, modestr = self.get_mode() st_mode, modestr = self.get_mode()
self.assertEqual(modestr[:3], '-rw') self.assertEqual(modestr[:3], '-rw')
self.assertS_IS("REG", st_mode) self.assertS_IS("REG", st_mode)
self.assertEqual(stat.S_IFMT(st_mode), self.assertEqual(self.statmod.S_IFMT(st_mode),
stat.S_IFREG) self.statmod.S_IFREG)
def test_directory(self): def test_directory(self):
os.mkdir(TESTFN) os.mkdir(TESTFN)
...@@ -162,25 +166,38 @@ class TestFilemode(unittest.TestCase): ...@@ -162,25 +166,38 @@ class TestFilemode(unittest.TestCase):
def test_module_attributes(self): def test_module_attributes(self):
for key, value in self.stat_struct.items(): for key, value in self.stat_struct.items():
modvalue = getattr(stat, key) modvalue = getattr(self.statmod, key)
self.assertEqual(value, modvalue, key) self.assertEqual(value, modvalue, key)
for key, value in self.permission_bits.items(): for key, value in self.permission_bits.items():
modvalue = getattr(stat, key) modvalue = getattr(self.statmod, key)
self.assertEqual(value, modvalue, key) self.assertEqual(value, modvalue, key)
for key in self.file_flags: for key in self.file_flags:
modvalue = getattr(stat, key) modvalue = getattr(self.statmod, key)
self.assertIsInstance(modvalue, int) self.assertIsInstance(modvalue, int)
for key in self.formats: for key in self.formats:
modvalue = getattr(stat, key) modvalue = getattr(self.statmod, key)
self.assertIsInstance(modvalue, int) self.assertIsInstance(modvalue, int)
for key in self.format_funcs: for key in self.format_funcs:
func = getattr(stat, key) func = getattr(self.statmod, key)
self.assertTrue(callable(func)) self.assertTrue(callable(func))
self.assertEqual(func(0), 0) self.assertEqual(func(0), 0)
class TestFilemodeCStat(TestFilemode):
statmod = c_stat
formats = TestFilemode.formats | {'S_IFDOOR', 'S_IFPORT', 'S_IFWHT'}
format_funcss = TestFilemode.format_funcs | {'S_ISDOOR', 'S_ISPORT',
'S_ISWHT'}
class TestFilemodePyStat(TestFilemode):
statmod = py_stat
def test_main(): def test_main():
run_unittest(TestFilemode) run_unittest(TestFilemodeCStat)
run_unittest(TestFilemodePyStat)
if __name__ == '__main__': if __name__ == '__main__':
test_main() test_main()
...@@ -123,6 +123,8 @@ Core and Builtins ...@@ -123,6 +123,8 @@ Core and Builtins
Library Library
------- -------
- Issue #11016: Add C implementation of the stat module as _stat.
- Issue #18248: Fix libffi build on AIX. - Issue #18248: Fix libffi build on AIX.
- Issue #18259: Declare sethostname in socketmodule.c for AIX - Issue #18259: Declare sethostname in socketmodule.c for AIX
......
...@@ -117,6 +117,7 @@ _operator _operator.c # operator.add() and similar goodies ...@@ -117,6 +117,7 @@ _operator _operator.c # operator.add() and similar goodies
_collections _collectionsmodule.c # Container types _collections _collectionsmodule.c # Container types
itertools itertoolsmodule.c # Functions creating iterators for efficient looping itertools itertoolsmodule.c # Functions creating iterators for efficient looping
atexit atexitmodule.c # Register functions to be run at interpreter-shutdown atexit atexitmodule.c # Register functions to be run at interpreter-shutdown
_stat _stat.c # stat.h interface
# access to ISO C locale support # access to ISO C locale support
_locale _localemodule.c # -lintl _locale _localemodule.c # -lintl
......
This diff is collapsed.
...@@ -1154,6 +1154,10 @@ ...@@ -1154,6 +1154,10 @@
RelativePath="..\..\Modules\signalmodule.c" RelativePath="..\..\Modules\signalmodule.c"
> >
</File> </File>
<File
RelativePath="..\..\Modules\_stat.c"
>
</File>
<File <File
RelativePath="..\..\Modules\symtablemodule.c" RelativePath="..\..\Modules\symtablemodule.c"
> >
......
...@@ -525,6 +525,7 @@ ...@@ -525,6 +525,7 @@
<ClCompile Include="..\Modules\sha256module.c" /> <ClCompile Include="..\Modules\sha256module.c" />
<ClCompile Include="..\Modules\sha512module.c" /> <ClCompile Include="..\Modules\sha512module.c" />
<ClCompile Include="..\Modules\signalmodule.c" /> <ClCompile Include="..\Modules\signalmodule.c" />
<ClCompile Include="..\Modules\_stat.c" />
<ClCompile Include="..\Modules\symtablemodule.c" /> <ClCompile Include="..\Modules\symtablemodule.c" />
<ClCompile Include="..\Modules\_threadmodule.c" /> <ClCompile Include="..\Modules\_threadmodule.c" />
<ClCompile Include="..\Modules\timemodule.c" /> <ClCompile Include="..\Modules\timemodule.c" />
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment