Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c77d9f38
Commit
c77d9f38
authored
Jun 22, 2013
by
Christian Heimes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #11016: Add C implementation of the stat module as _stat
parent
6ce8d17d
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
679 additions
and
23 deletions
+679
-23
Doc/library/stat.rst
Doc/library/stat.rst
+50
-1
Doc/whatsnew/3.4.rst
Doc/whatsnew/3.4.rst
+6
-1
Lib/stat.py
Lib/stat.py
+6
-0
Lib/test/test_stat.py
Lib/test/test_stat.py
+37
-20
Misc/NEWS
Misc/NEWS
+2
-0
Modules/Setup.dist
Modules/Setup.dist
+1
-0
Modules/_stat.c
Modules/_stat.c
+571
-0
PC/VS9.0/pythoncore.vcproj
PC/VS9.0/pythoncore.vcproj
+4
-0
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj
+2
-1
No files found.
Doc/library/stat.rst
View file @
c77d9f38
...
...
@@ -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`
:source:`Lib/stat.py`
--------------
...
...
@@ -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
: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
types:
...
...
@@ -53,6 +57,24 @@ types:
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
mode:
...
...
@@ -113,6 +135,10 @@ readable string:
.. 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
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:
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`:
.. data:: S_ISUID
...
...
Doc/whatsnew/3.4.rst
View file @
c77d9f38
...
...
@@ -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
=============
...
...
Lib/stat.py
View file @
c77d9f38
...
...
@@ -147,3 +147,9 @@ def filemode(mode):
else
:
perm
.
append
(
"-"
)
return
""
.
join
(
perm
)
# If available, use C implementation
try
:
from
_stat
import
*
except
ModuleNotFoundError
:
pass
Lib/test/test_stat.py
View file @
c77d9f38
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
):
statmod
=
None
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
modestr
=
s
tat
.
filemode
(
st_mode
)
modestr
=
s
elf
.
statmod
.
filemode
(
st_mode
)
return
st_mode
,
modestr
def
assertS_IS
(
self
,
name
,
mode
):
# test format, lstrip is for S_IFIFO
fmt
=
getattr
(
s
tat
,
"S_IF"
+
name
.
lstrip
(
"F"
))
self
.
assertEqual
(
s
tat
.
S_IFMT
(
mode
),
fmt
)
fmt
=
getattr
(
s
elf
.
statmod
,
"S_IF"
+
name
.
lstrip
(
"F"
))
self
.
assertEqual
(
s
elf
.
statmod
.
S_IFMT
(
mode
),
fmt
)
# test that just one function returns true
testname
=
"S_IS"
+
name
for
funcname
in
self
.
format_funcs
:
func
=
getattr
(
s
tat
,
funcname
,
None
)
func
=
getattr
(
s
elf
.
statmod
,
funcname
,
None
)
if
func
is
None
:
if
funcname
==
testname
:
raise
ValueError
(
funcname
)
...
...
@@ -88,35 +92,35 @@ class TestFilemode(unittest.TestCase):
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
,
'-rwx------'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
s
tat
.
S_IMODE
(
st_mode
),
s
tat
.
S_IRWXU
)
self
.
assertEqual
(
s
elf
.
statmod
.
S_IMODE
(
st_mode
),
s
elf
.
statmod
.
S_IRWXU
)
os
.
chmod
(
TESTFN
,
0o070
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
,
'----rwx---'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
s
tat
.
S_IMODE
(
st_mode
),
s
tat
.
S_IRWXG
)
self
.
assertEqual
(
s
elf
.
statmod
.
S_IMODE
(
st_mode
),
s
elf
.
statmod
.
S_IRWXG
)
os
.
chmod
(
TESTFN
,
0o007
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
,
'-------rwx'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
s
tat
.
S_IMODE
(
st_mode
),
s
tat
.
S_IRWXO
)
self
.
assertEqual
(
s
elf
.
statmod
.
S_IMODE
(
st_mode
),
s
elf
.
statmod
.
S_IRWXO
)
os
.
chmod
(
TESTFN
,
0o444
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
modestr
,
'-r--r--r--'
)
self
.
assertEqual
(
s
tat
.
S_IMODE
(
st_mode
),
0o444
)
self
.
assertEqual
(
s
elf
.
statmod
.
S_IMODE
(
st_mode
),
0o444
)
else
:
os
.
chmod
(
TESTFN
,
0o700
)
st_mode
,
modestr
=
self
.
get_mode
()
self
.
assertEqual
(
modestr
[:
3
],
'-rw'
)
self
.
assertS_IS
(
"REG"
,
st_mode
)
self
.
assertEqual
(
s
tat
.
S_IFMT
(
st_mode
),
s
tat
.
S_IFREG
)
self
.
assertEqual
(
s
elf
.
statmod
.
S_IFMT
(
st_mode
),
s
elf
.
statmod
.
S_IFREG
)
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
():
modvalue
=
getattr
(
s
tat
,
key
)
modvalue
=
getattr
(
s
elf
.
statmod
,
key
)
self
.
assertEqual
(
value
,
modvalue
,
key
)
for
key
,
value
in
self
.
permission_bits
.
items
():
modvalue
=
getattr
(
s
tat
,
key
)
modvalue
=
getattr
(
s
elf
.
statmod
,
key
)
self
.
assertEqual
(
value
,
modvalue
,
key
)
for
key
in
self
.
file_flags
:
modvalue
=
getattr
(
s
tat
,
key
)
modvalue
=
getattr
(
s
elf
.
statmod
,
key
)
self
.
assertIsInstance
(
modvalue
,
int
)
for
key
in
self
.
formats
:
modvalue
=
getattr
(
s
tat
,
key
)
modvalue
=
getattr
(
s
elf
.
statmod
,
key
)
self
.
assertIsInstance
(
modvalue
,
int
)
for
key
in
self
.
format_funcs
:
func
=
getattr
(
s
tat
,
key
)
func
=
getattr
(
s
elf
.
statmod
,
key
)
self
.
assertTrue
(
callable
(
func
))
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
():
run_unittest
(
TestFilemode
)
run_unittest
(
TestFilemodeCStat
)
run_unittest
(
TestFilemodePyStat
)
if
__name__
==
'__main__'
:
test_main
()
Misc/NEWS
View file @
c77d9f38
...
...
@@ -123,6 +123,8 @@ Core and Builtins
Library
-------
-
Issue
#
11016
:
Add
C
implementation
of
the
stat
module
as
_stat
.
-
Issue
#
18248
:
Fix
libffi
build
on
AIX
.
-
Issue
#
18259
:
Declare
sethostname
in
socketmodule
.
c
for
AIX
...
...
Modules/Setup.dist
View file @
c77d9f38
...
...
@@ -117,6 +117,7 @@ _operator _operator.c # operator.add() and similar goodies
_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
...
...
Modules/_stat.c
0 → 100644
View file @
c77d9f38
This diff is collapsed.
Click to expand it.
PC/VS9.0/pythoncore.vcproj
View file @
c77d9f38
...
...
@@ -1154,6 +1154,10 @@
RelativePath=
"..\..\Modules\signalmodule.c"
>
</File>
<File
RelativePath=
"..\..\Modules\_stat.c"
>
</File>
<File
RelativePath=
"..\..\Modules\symtablemodule.c"
>
...
...
PCbuild/pythoncore.vcxproj
View file @
c77d9f38
...
...
@@ -525,6 +525,7 @@
<ClCompile
Include=
"..\Modules\sha256module.c"
/>
<ClCompile
Include=
"..\Modules\sha512module.c"
/>
<ClCompile
Include=
"..\Modules\signalmodule.c"
/>
<ClCompile
Include=
"..\Modules\_stat.c"
/>
<ClCompile
Include=
"..\Modules\symtablemodule.c"
/>
<ClCompile
Include=
"..\Modules\_threadmodule.c"
/>
<ClCompile
Include=
"..\Modules\timemodule.c"
/>
...
...
@@ -678,4 +679,4 @@
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.targets"
/>
<ImportGroup
Label=
"ExtensionTargets"
>
</ImportGroup>
</Project>
\ No newline at end of file
</Project>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment