Skip to content

Commit 3bc034b

Browse files
committed
Use bitwise ops instead of %; added some warnings and comments
1 parent e58f98b commit 3bc034b

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

Lib/stat.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Module 'stat'
2-
2+
#
33
# Defines constants and functions for interpreting stat/lstat struct
4-
# as returned by posix.stat() and posix.lstat() (if it exists).
5-
6-
# XXX This module may have to be adapted for UNIXoid systems whose
7-
# <sys/stat.h> deviates from AT&T or BSD UNIX; their S_IF* constants
8-
# may differ.
9-
4+
# as returned by os.stat() and os.lstat() (if it exists).
5+
#
106
# Suggested usage: from stat import *
7+
#
8+
# XXX Strictly spoken, this module may have to be adapted for each POSIX
9+
# implementation; in practice, however, the numeric constants used by
10+
# stat() are almost universal (even for stat() emulations on non-UNIX
11+
# systems like Macintosh or MS-DOS).
1112

12-
# Tuple indices for stat struct members
13+
# Indices for stat struct members in tuple returned by os.stat()
1314

1415
ST_MODE = 0
1516
ST_INO = 1
@@ -22,10 +23,16 @@
2223
ST_MTIME = 8
2324
ST_CTIME = 9
2425

26+
# Extract bits from the mode
27+
2528
def S_IMODE(mode):
26-
return mode%4096
29+
return mode & 07777
30+
2731
def S_IFMT(mode):
28-
return mode - mode%4096
32+
return mode & ~07777
33+
34+
# Constants used as S_IFMT() for various file types
35+
# (not all are implemented on all systems)
2936

3037
S_IFDIR = 0040000
3138
S_IFCHR = 0020000
@@ -35,6 +42,8 @@ def S_IFMT(mode):
3542
S_IFLNK = 0120000
3643
S_IFSOCK = 0140000
3744

45+
# Functions to test for each file type
46+
3847
def S_ISDIR(mode):
3948
return S_IFMT(mode) == S_IFDIR
4049

0 commit comments

Comments
 (0)