Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40d9304

Browse files
committed
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
1 parent 6b47ed1 commit 40d9304

4 files changed

Lines changed: 17 additions & 36 deletions

File tree

Lib/cmpcache.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
# - Files with different type or size cannot be identical
99
# - We keep a cache of outcomes of earlier comparisons
1010
# - We don't fork a process to run 'cmp' but read the files ourselves
11-
#
12-
# XXX There is a dependency on constants in <sys/stat.h> here.
1311

1412
import posix
13+
import stat
1514
import statcache
1615

1716

@@ -27,7 +26,7 @@ def cmp(f1, f2):
2726
# Return 1 for identical files, 0 for different.
2827
# Raise exceptions if either file could not be statted, read, etc.
2928
s1, s2 = sig(statcache.stat(f1)), sig(statcache.stat(f2))
30-
if s1[0] <> 8 or s2[0] <> 8: # XXX 8 is S_IFREG in <sys/stat.h>
29+
if not stat.S_ISREG(s1[0]) or not stat.S_ISREG(s2[0]):
3130
# Either is a not a plain file -- always report as different
3231
return 0
3332
if s1 = s2:
@@ -53,12 +52,7 @@ def cmp(f1, f2):
5352
# Return signature (i.e., type, size, mtime) from raw stat data.
5453
#
5554
def sig(st):
56-
# 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid
57-
# 6-9: st_size, st_atime, st_mtime, st_ctime
58-
type = st[0] / 4096 # XXX dependent on S_IFMT in <sys/stat.h>
59-
size = st[6]
60-
mtime = st[8]
61-
return type, size, mtime
55+
return stat.S_IFMT(st[ST_MODE]), st[stat.ST_SIZE], st[stat.ST_MTIME]
6256

6357
# Compare two files, really.
6458
#

Lib/dircmp.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@
99
import dircache
1010
import cmpcache
1111
import statcache
12-
13-
14-
# File type constants from <sys/stat.h>.
15-
#
16-
S_IFDIR = 4
17-
S_IFREG = 8
18-
19-
# Extract the file type from a stat buffer.
20-
#
21-
def S_IFMT(st): return st[0] / 4096
22-
12+
from stat import *
2313

2414
# Directory comparison class.
2515
#
@@ -79,13 +69,13 @@ def phase2(dd): # Distinguish files, directories, funnies
7969
ok = 0
8070
#
8171
if ok:
82-
a_type = S_IFMT(a_stat)
83-
b_type = S_IFMT(b_stat)
72+
a_type = S_IFMT(a_stat[ST_MODE])
73+
b_type = S_IFMT(b_stat[ST_MODE])
8474
if a_type <> b_type:
8575
dd.common_funny.append(x)
86-
elif a_type = S_IFDIR:
76+
elif S_ISDIR(a_type):
8777
dd.common_dirs.append(x)
88-
elif a_type = S_IFREG:
78+
elif S_ISREG(a_type):
8979
dd.common_files.append(x)
9080
else:
9181
dd.common_funny.append(x)

Lib/posixpath.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Module 'path' -- common operations on POSIX pathnames
22

33
import posix
4+
import stat
45

56

67
# Intelligent pathname concatenation.
@@ -63,7 +64,7 @@ def isdir(path):
6364
st = posix.stat(path)
6465
except posix.error:
6566
return 0
66-
return st[0] / 4096 = 4 # S_IFDIR
67+
return stat.S_ISDIR(st[stat.ST_MODE])
6768

6869

6970
# Is a path a symbolic link?
@@ -74,7 +75,7 @@ def islink(path):
7475
st = posix.lstat(path)
7576
except (posix.error, NameError):
7677
return 0
77-
return st[0] / 4096 = 10 # S_IFLNK
78+
return stat.S_ISLNK(st[stat.ST_MODE])
7879

7980

8081
_mounts = []

Lib/statcache.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# There are functions to reset the cache or to selectively remove items.
55

66
import posix
7-
7+
from stat import *
88

99
# The cache.
1010
# Keys are pathnames, values are `posix.stat' outcomes.
@@ -15,10 +15,8 @@
1515
# Stat a file, possibly out of the cache.
1616
#
1717
def stat(path):
18-
try:
18+
if cache.has_key(path):
1919
return cache[path]
20-
except RuntimeError:
21-
pass
2220
cache[path] = ret = posix.stat(path)
2321
return ret
2422

@@ -37,10 +35,8 @@ def reset():
3735
# Remove a given item from the cache, if it exists.
3836
#
3937
def forget(path):
40-
try:
38+
if cache.has_key(path):
4139
del cache[path]
42-
except RuntimeError:
43-
pass
4440

4541

4642
# Remove all pathnames with a given prefix.
@@ -84,7 +80,7 @@ def forget_except_prefix(prefix):
8480
#
8581
def isdir(path):
8682
try:
87-
# mode is st[0]; type is mode/4096; S_IFDIR is 4
88-
return stat(path)[0] / 4096 = 4
89-
except RuntimeError:
83+
st = stat(path)
84+
except posix.error:
9085
return 0
86+
return S_ISDIR(st[ST_MODE])

0 commit comments

Comments
 (0)