cpython: 420f70a22b9d (original) (raw)

Mercurial > cpython

changeset 84258:420f70a22b9d

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

Christian Heimes christian@cheimes.de
date Sat, 22 Jun 2013 21:05:02 +0200
parents d7d73f48dfda
children 1d67c0893719
files Doc/library/stat.rst Doc/whatsnew/3.4.rst Lib/stat.py Lib/test/test_stat.py Misc/NEWS Modules/Setup.dist Modules/_stat.c PC/VS9.0/pythoncore.vcproj PCbuild/pythoncore.vcxproj
diffstat 9 files changed, 679 insertions(+), 23 deletions(-)[+] [-] Doc/library/stat.rst 51 Doc/whatsnew/3.4.rst 7 Lib/stat.py 6 Lib/test/test_stat.py 57 Misc/NEWS 2 Modules/Setup.dist 1 Modules/_stat.c 571 PC/VS9.0/pythoncore.vcproj 4 PCbuild/pythoncore.vcxproj 3

line wrap: on

line diff

--- a/Doc/library/stat.rst +++ b/Doc/library/stat.rst @@ -6,7 +6,8 @@ os.lstat() and os.fstat(). .. sectionauthor:: Skip Montanaro skip@automatrix.com -Source code: :source:Lib/stat.py +Source code: :source:Modules/_stat.c

-------------- @@ -15,6 +16,9 @@ results of :func:os.stat, :func:os.fs[](#l1.13) exist). For complete details about the :c:func:stat, :c:func:fstat and[](#l1.14) :c:func:lstat` calls, consult the documentation for your system. +.. versionchanged:: 3.4

The :mod:stat module defines the following functions to test for specific file types: @@ -53,6 +57,24 @@ types: Return non-zero if the mode is from a socket. +.. function:: S_ISDOOR(mode) +

+.. function:: S_ISPORT(mode) +

+.. function:: S_ISWHT(mode) +

Two additional functions are defined for more general manipulation of the file's mode: @@ -113,6 +135,10 @@ readable string: .. versionadded:: 3.3

+ All the variables below are simply symbolic indexes into the 10-tuple returned by :func:os.stat, :func:os.fstat or :func:os.lstat. @@ -210,6 +236,29 @@ Use of the functions above is more porta FIFO. +.. data:: S_IFDOOR +

+.. data:: S_IFPORT +

+.. data:: S_IFWHT +

+.. note:: +

The following flags can also be used in the mode argument of :func:os.chmod: .. data:: S_ISUID

--- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -182,7 +182,6 @@ functools New :func:functools.singledispatch decorator: see the :pep:443. - smtplib ------- @@ -213,6 +212,12 @@ wave The :meth:~wave.getparams method now returns a namedtuple rather than a 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 =============

--- a/Lib/stat.py +++ b/Lib/stat.py @@ -147,3 +147,9 @@ def filemode(mode): else: perm.append("-") return "".join(perm) + +# If available, use C implementation +try:

+except ModuleNotFoundError:

--- a/Lib/test/test_stat.py +++ b/Lib/test/test_stat.py @@ -1,9 +1,13 @@ import unittest import os 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):

+ file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK', 'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN', 'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'} @@ -60,17 +64,17 @@ class TestFilemode(unittest.TestCase): def get_mode(self, fname=TESTFN): st_mode = os.lstat(fname).st_mode

def assertS_IS(self, name, mode): # test format, lstrip is for S_IFIFO

@@ -88,35 +92,35 @@ class TestFilemode(unittest.TestCase): st_mode, modestr = self.get_mode() self.assertEqual(modestr, '-rwx------') self.assertS_IS("REG", st_mode)

os.chmod(TESTFN, 0o070) st_mode, modestr = self.get_mode() self.assertEqual(modestr, '----rwx---') self.assertS_IS("REG", st_mode)

os.chmod(TESTFN, 0o007) st_mode, modestr = self.get_mode() self.assertEqual(modestr, '-------rwx') self.assertS_IS("REG", st_mode)

os.chmod(TESTFN, 0o444) st_mode, modestr = self.get_mode() self.assertS_IS("REG", st_mode) self.assertEqual(modestr, '-r--r--r--')

def test_directory(self): os.mkdir(TESTFN) @@ -162,25 +166,38 @@ class TestFilemode(unittest.TestCase): def test_module_attributes(self): for key, value in self.stat_struct.items():

+class TestFilemodeCStat(TestFilemode):

+

+ + +class TestFilemodePyStat(TestFilemode):

+ + def test_main():

if name == 'main': test_main()

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -123,6 +123,8 @@ Core and Builtins Library ------- +- Issue #11016: Add C implementation of the stat module as _stat. +

--- a/Modules/Setup.dist +++ b/Modules/Setup.dist @@ -117,6 +117,7 @@ pwd pwdmodule.c # this is needed to fi _collections _collectionsmodule.c # Container types itertools itertoolsmodule.c # Functions creating iterators for efficient looping atexit atexitmodule.c # Register functions to be run at interpreter-shutdown +_stat _stat.c # stat.h interface

access to ISO C locale support

_locale _localemodule.c # -lintl

new file mode 100644 --- /dev/null +++ b/Modules/_stat.c @@ -0,0 +1,571 @@ +/* stat.h interface

+ +stat_S_ISFUNC(S_ISDIR,

+ +stat_S_ISFUNC(S_ISCHR,

+ +stat_S_ISFUNC(S_ISBLK,

+ +stat_S_ISFUNC(S_ISREG,

+ +stat_S_ISFUNC(S_ISFIFO,

+ +stat_S_ISFUNC(S_ISLNK,

+ +stat_S_ISFUNC(S_ISSOCK,

+ +stat_S_ISFUNC(S_ISDOOR,

+ +stat_S_ISFUNC(S_ISPORT,

+ +stat_S_ISFUNC(S_ISWHT,

+ + +PyDoc_STRVAR(stat_S_IMODE_doc, +"Return the portion of the file's mode that can be set by os.chmod()."); + +static PyObject * +stat_S_IMODE(PyObject *self, PyObject *omode) +{

+} + + +PyDoc_STRVAR(stat_S_IFMT_doc, +"Return the portion of the file's mode that describes the file type."); + +static PyObject * +stat_S_IFMT(PyObject *self, PyObject *omode) +{

+} + +/* file type chars according to

+static char +filetype(mode_t mode) +{

+} + +static void +fileperm(mode_t mode, char *buf) +{

+} + +PyDoc_STRVAR(stat_filemode_doc, +"Convert a file's mode to a string of the form '-rwxrwxrwx'"); + +static PyObject * +stat_filemode(PyObject *self, PyObject *omode) +{

+

+

+} + + +static PyMethodDef stat_methods[] = {

+}; + + +PyDoc_STRVAR(module_doc, +"S_IFMT_: file type bits\n[](#l7.430) +S_IFDIR: directory\n[](#l7.431) +S_IFCHR: character device\n[](#l7.432) +S_IFBLK: block device\n[](#l7.433) +S_IFREG: regular file\n[](#l7.434) +S_IFIFO: fifo (named pipe)\n[](#l7.435) +S_IFLNK: symbolic link\n[](#l7.436) +S_IFSOCK: socket file\n[](#l7.437) +S_IFDOOR: door\n[](#l7.438) +S_IFPORT: event port\n[](#l7.439) +S_IFWHT: whiteout\n[](#l7.440) +\n" + +"S_ISUID: set UID bit\n[](#l7.443) +S_ISGID: set GID bit\n[](#l7.444) +S_ENFMT: file locking enforcement\n[](#l7.445) +S_ISVTX: sticky bit\n[](#l7.446) +S_IREAD: Unix V7 synonym for S_IRUSR\n[](#l7.447) +S_IWRITE: Unix V7 synonym for S_IWUSR\n[](#l7.448) +S_IEXEC: Unix V7 synonym for S_IXUSR\n[](#l7.449) +S_IRWXU: mask for owner permissions\n[](#l7.450) +S_IRUSR: read by owner\n[](#l7.451) +S_IWUSR: write by owner\n[](#l7.452) +S_IXUSR: execute by owner\n[](#l7.453) +S_IRWXG: mask for group permissions\n[](#l7.454) +S_IRGRP: read by group\n[](#l7.455) +S_IWGRP: write by group\n[](#l7.456) +S_IXGRP: execute by group\n[](#l7.457) +S_IRWXO: mask for others (not in group) permissions\n[](#l7.458) +S_IROTH: read by others\n[](#l7.459) +S_IWOTH: write by others\n[](#l7.460) +S_IXOTH: execute by others\n[](#l7.461) +\n" + +"UF_NODUMP: do not dump file\n[](#l7.464) +UF_IMMUTABLE: file may not be changed\n[](#l7.465) +UF_APPEND: file may only be appended to\n[](#l7.466) +UF_OPAQUE: directory is opaque when viewed through a union stack\n[](#l7.467) +UF_NOUNLINK: file may not be renamed or deleted\n[](#l7.468) +UF_COMPRESSED: OS X: file is hfs-compressed\n[](#l7.469) +UF_HIDDEN: OS X: file should not be displayed\n[](#l7.470) +SF_ARCHIVED: file may be archived\n[](#l7.471) +SF_IMMUTABLE: file may not be changed\n[](#l7.472) +SF_APPEND: file may only be appended to\n[](#l7.473) +SF_NOUNLINK: file may not be renamed or deleted\n[](#l7.474) +SF_SNAPSHOT: file is a snapshot file\n[](#l7.475) +\n" + +"ST_MODE\n[](#l7.478) +ST_INO\n[](#l7.479) +ST_DEV\n[](#l7.480) +ST_NLINK\n[](#l7.481) +ST_UID\n[](#l7.482) +ST_GID\n[](#l7.483) +ST_SIZE\n[](#l7.484) +ST_ATIME\n[](#l7.485) +ST_MTIME\n[](#l7.486) +ST_CTIME\n[](#l7.487) +"); + + +static struct PyModuleDef statmodule = {

+}; + +PyMODINIT_FUNC +PyInit__stat(void) +{

+

+

+

+

+

+

+

+

+

+} + +#ifdef __cplusplus +} +#endif

--- a/PC/VS9.0/pythoncore.vcproj +++ b/PC/VS9.0/pythoncore.vcproj @@ -1155,6 +1155,10 @@ > <File + RelativePath="....\Modules_stat.c" + > + + <File RelativePath="....\Modules\symtablemodule.c" >

--- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -525,6 +525,7 @@