Replaced obsolete stat module constants with equivalent attributes

This commit is contained in:
Raymond Hettinger 2002-06-01 19:51:15 +00:00
parent 16e3c427f3
commit 32200aeac6
18 changed files with 59 additions and 79 deletions

View File

@ -308,7 +308,7 @@ def executable(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return st[0] & 0111 != 0 return st.st_mode & 0111 != 0
def test(HandlerClass = CGIHTTPRequestHandler, def test(HandlerClass = CGIHTTPRequestHandler,

View File

@ -13,7 +13,6 @@ See module py_compile for details of the actual byte-compilation.
""" """
import os import os
import stat
import sys import sys
import py_compile import py_compile
@ -56,8 +55,8 @@ def compile_dir(dir, maxlevels=10, ddir=None,
head, tail = name[:-3], name[-3:] head, tail = name[:-3], name[-3:]
if tail == '.py': if tail == '.py':
cfile = fullname + (__debug__ and 'c' or 'o') cfile = fullname + (__debug__ and 'c' or 'o')
ftime = os.stat(fullname)[stat.ST_MTIME] ftime = os.stat(fullname).st_mtime
try: ctime = os.stat(cfile)[stat.ST_MTIME] try: ctime = os.stat(cfile).st_mtime
except os.error: ctime = 0 except os.error: ctime = 0
if (ctime > ftime) and not force: continue if (ctime > ftime) and not force: continue
if not quiet: if not quiet:

View File

@ -23,7 +23,7 @@ def listdir(path):
except KeyError: except KeyError:
cached_mtime, list = -1, [] cached_mtime, list = -1, []
try: try:
mtime = os.stat(path)[8] mtime = os.stat(path).st_mtime
except os.error: except os.error:
return [] return []
if mtime != cached_mtime: if mtime != cached_mtime:

View File

@ -123,19 +123,16 @@ def commonprefix(m):
def getsize(filename): def getsize(filename):
"""Return the size of a file, reported by os.stat().""" """Return the size of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_size
return st[stat.ST_SIZE]
def getmtime(filename): def getmtime(filename):
"""Return the last modification time of a file, reported by os.stat().""" """Return the last modification time of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_mtime
return st[stat.ST_MTIME]
def getatime(filename): def getatime(filename):
"""Return the last access time of a file, reported by os.stat().""" """Return the last access time of a file, reported by os.stat()."""
st = os.stat(filename)
return st[stat.ST_ATIME]
return os.stat(filename).st_atime
def islink(path): def islink(path):
"""Is a path a symbolic link? """Is a path a symbolic link?
@ -162,7 +159,7 @@ def isdir(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISDIR(st[stat.ST_MODE]) return stat.S_ISDIR(st.st_mode)
def isfile(path): def isfile(path):
@ -172,7 +169,7 @@ def isfile(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISREG(st[stat.ST_MODE]) return stat.S_ISREG(st.st_mode)
def ismount(path): def ismount(path):

View File

@ -64,9 +64,9 @@ def cmp(f1, f2, shallow=1, use_statcache=0):
return outcome return outcome
def _sig(st): def _sig(st):
return (stat.S_IFMT(st[stat.ST_MODE]), return (stat.S_IFMT(st.st_mode),
st[stat.ST_SIZE], st.st_size,
st[stat.ST_MTIME]) st.st_mtime)
def _do_cmp(f1, f2): def _do_cmp(f1, f2):
bufsize = BUFSIZE bufsize = BUFSIZE
@ -199,8 +199,8 @@ class dircmp:
ok = 0 ok = 0
if ok: if ok:
a_type = stat.S_IFMT(a_stat[stat.ST_MODE]) a_type = stat.S_IFMT(a_stat.st_mode)
b_type = stat.S_IFMT(b_stat[stat.ST_MODE]) b_type = stat.S_IFMT(b_stat.st_mode)
if a_type != b_type: if a_type != b_type:
self.common_funny.append(x) self.common_funny.append(x)
elif stat.S_ISDIR(a_type): elif stat.S_ISDIR(a_type):

View File

@ -484,7 +484,7 @@ def _os_path_isdir(pathname):
s = _os_stat(pathname) s = _os_stat(pathname)
except OSError: except OSError:
return None return None
return (s[0] & 0170000) == 0040000 return (s.st_mode & 0170000) == 0040000
def _timestamp(pathname): def _timestamp(pathname):
"Return the file modification time as a Long." "Return the file modification time as a Long."
@ -492,7 +492,7 @@ def _timestamp(pathname):
s = _os_stat(pathname) s = _os_stat(pathname)
except OSError: except OSError:
return None return None
return long(s[8]) return long(s.st_mtime)
###################################################################### ######################################################################

View File

@ -7,7 +7,6 @@ that name.
import sys import sys
import os import os
from stat import *
__all__ = ["getline","clearcache","checkcache"] __all__ = ["getline","clearcache","checkcache"]
@ -52,7 +51,7 @@ def checkcache():
except os.error: except os.error:
del cache[filename] del cache[filename]
continue continue
if size != stat[ST_SIZE] or mtime != stat[ST_MTIME]: if size != stat.st_size or mtime != stat.st_mtime:
del cache[filename] del cache[filename]
@ -96,6 +95,6 @@ def updatecache(filename):
except IOError, msg: except IOError, msg:
## print '*** Cannot open', fullname, ':', msg ## print '*** Cannot open', fullname, ':', msg
return [] return []
size, mtime = stat[ST_SIZE], stat[ST_MTIME] size, mtime = stat.st_size, stat.st_mtime
cache[filename] = size, mtime, lines, fullname cache[filename] = size, mtime, lines, fullname
return lines return lines

View File

@ -100,25 +100,22 @@ def isdir(s):
st = os.stat(s) st = os.stat(s)
except os.error: except os.error:
return 0 return 0
return S_ISDIR(st[ST_MODE]) return S_ISDIR(st.st_mode)
# Get size, mtime, atime of files. # Get size, mtime, atime of files.
def getsize(filename): def getsize(filename):
"""Return the size of a file, reported by os.stat().""" """Return the size of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_size
return st[ST_SIZE]
def getmtime(filename): def getmtime(filename):
"""Return the last modification time of a file, reported by os.stat().""" """Return the last modification time of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_mtime
return st[ST_MTIME]
def getatime(filename): def getatime(filename):
"""Return the last access time of a file, reported by os.stat().""" """Return the last access time of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_atime
return st[ST_ATIME]
def islink(s): def islink(s):
@ -138,7 +135,7 @@ def isfile(s):
st = os.stat(s) st = os.stat(s)
except os.error: except os.error:
return False return False
return S_ISREG(st[ST_MODE]) return S_ISREG(st.st_mode)
def exists(s): def exists(s):

View File

@ -74,7 +74,6 @@ FOLDER_PROTECT = 0700
import os import os
import sys import sys
from stat import ST_NLINK
import re import re
import mimetools import mimetools
import multifile import multifile
@ -155,8 +154,7 @@ class MH:
fullname = os.path.join(self.path, name) fullname = os.path.join(self.path, name)
# Get the link count so we can avoid listing folders # Get the link count so we can avoid listing folders
# that have no subfolders. # that have no subfolders.
st = os.stat(fullname) nlinks = os.stat(fullname).st_nlink
nlinks = st[ST_NLINK]
if nlinks <= 2: if nlinks <= 2:
return [] return []
subfolders = [] subfolders = []
@ -183,8 +181,7 @@ class MH:
fullname = os.path.join(self.path, name) fullname = os.path.join(self.path, name)
# Get the link count so we can avoid listing folders # Get the link count so we can avoid listing folders
# that have no subfolders. # that have no subfolders.
st = os.stat(fullname) nlinks = os.stat(fullname).st_nlink
nlinks = st[ST_NLINK]
if nlinks <= 2: if nlinks <= 2:
return [] return []
subfolders = [] subfolders = []

View File

@ -216,18 +216,15 @@ def commonprefix(m):
def getsize(filename): def getsize(filename):
"""Return the size of a file, reported by os.stat()""" """Return the size of a file, reported by os.stat()"""
st = os.stat(filename) return os.stat(filename).st_size
return st[stat.ST_SIZE]
def getmtime(filename): def getmtime(filename):
"""Return the last modification time of a file, reported by os.stat()""" """Return the last modification time of a file, reported by os.stat()"""
st = os.stat(filename) return os.stat(filename).st_mtime
return st[stat.ST_MTIME]
def getatime(filename): def getatime(filename):
"""Return the last access time of a file, reported by os.stat()""" """Return the last access time of a file, reported by os.stat()"""
st = os.stat(filename) return os.stat(filename).st_atime
return st[stat.ST_ATIME]
# Is a path a symbolic link? # Is a path a symbolic link?
@ -260,7 +257,7 @@ def isdir(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISDIR(st[stat.ST_MODE]) return stat.S_ISDIR(st.st_mode)
# Is a path a regular file? # Is a path a regular file?
@ -273,7 +270,7 @@ def isfile(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISREG(st[stat.ST_MODE]) return stat.S_ISREG(st.st_mode)
# Is a path a mount point? Either a root (with or without drive letter) # Is a path a mount point? Either a root (with or without drive letter)

View File

@ -175,18 +175,15 @@ def commonprefix(m):
def getsize(filename): def getsize(filename):
"""Return the size of a file, reported by os.stat()""" """Return the size of a file, reported by os.stat()"""
st = os.stat(filename) return os.stat(filename).st_size
return st[stat.ST_SIZE]
def getmtime(filename): def getmtime(filename):
"""Return the last modification time of a file, reported by os.stat()""" """Return the last modification time of a file, reported by os.stat()"""
st = os.stat(filename) return os.stat(filename).st_mtime
return st[stat.ST_MTIME]
def getatime(filename): def getatime(filename):
"""Return the last access time of a file, reported by os.stat()""" """Return the last access time of a file, reported by os.stat()"""
st = os.stat(filename) return os.stat(filename).st_atime
return st[stat.ST_ATIME]
# Is a path a symbolic link? # Is a path a symbolic link?
@ -217,7 +214,7 @@ def isdir(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISDIR(st[stat.ST_MODE]) return stat.S_ISDIR(st.st_mode)
# Is a path a regular file? # Is a path a regular file?
@ -230,7 +227,7 @@ def isfile(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISREG(st[stat.ST_MODE]) return stat.S_ISREG(st.st_mode)
# Is a path a mount point? Either a root (with or without drive letter) # Is a path a mount point? Either a root (with or without drive letter)

View File

@ -136,18 +136,15 @@ def commonprefix(m):
def getsize(filename): def getsize(filename):
"""Return the size of a file, reported by os.stat().""" """Return the size of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_size
return st[stat.ST_SIZE]
def getmtime(filename): def getmtime(filename):
"""Return the last modification time of a file, reported by os.stat().""" """Return the last modification time of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_mtime
return st[stat.ST_MTIME]
def getatime(filename): def getatime(filename):
"""Return the last access time of a file, reported by os.stat().""" """Return the last access time of a file, reported by os.stat()."""
st = os.stat(filename) return os.stat(filename).st_atime
return st[stat.ST_ATIME]
# Is a path a symbolic link? # Is a path a symbolic link?
@ -159,7 +156,7 @@ def islink(path):
st = os.lstat(path) st = os.lstat(path)
except (os.error, AttributeError): except (os.error, AttributeError):
return False return False
return stat.S_ISLNK(st[stat.ST_MODE]) return stat.S_ISLNK(st.st_mode)
# Does a path exist? # Does a path exist?
@ -184,7 +181,7 @@ def isdir(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISDIR(st[stat.ST_MODE]) return stat.S_ISDIR(st.st_mode)
# Is a path a regular file? # Is a path a regular file?
@ -197,7 +194,7 @@ def isfile(path):
st = os.stat(path) st = os.stat(path)
except os.error: except os.error:
return False return False
return stat.S_ISREG(st[stat.ST_MODE]) return stat.S_ISREG(st.st_mode)
# Are two filenames really pointing to the same file? # Are two filenames really pointing to the same file?
@ -224,8 +221,8 @@ def sameopenfile(fp1, fp2):
def samestat(s1, s2): def samestat(s1, s2):
"""Test whether two stat buffers reference the same file""" """Test whether two stat buffers reference the same file"""
return s1[stat.ST_INO] == s2[stat.ST_INO] and \ return s1.st_ino == s2.st_ino and \
s1[stat.ST_DEV] == s2[stat.ST_DEV] s1.st_dev == s2.st_dev
# Is a path a mount point? # Is a path a mount point?
@ -238,12 +235,12 @@ def ismount(path):
s2 = os.stat(join(path, '..')) s2 = os.stat(join(path, '..'))
except os.error: except os.error:
return False # It doesn't exist -- so not a mount point :-) return False # It doesn't exist -- so not a mount point :-)
dev1 = s1[stat.ST_DEV] dev1 = s1.st_dev
dev2 = s2[stat.ST_DEV] dev2 = s2.st_dev
if dev1 != dev2: if dev1 != dev2:
return True # path/.. on a different device as path return True # path/.. on a different device as path
ino1 = s1[stat.ST_INO] ino1 = s1.st_ino
ino2 = s2[stat.ST_INO] ino2 = s2.st_ino
if ino1 == ino2: if ino1 == ino2:
return True # path/.. is the same i-node as path return True # path/.. is the same i-node as path
return False return False

View File

@ -108,7 +108,7 @@ class Stats:
f.close() f.close()
try: try:
file_stats = os.stat(arg) file_stats = os.stat(arg)
arg = time.ctime(file_stats[8]) + " " + arg arg = time.ctime(file_stats.st_mtime) + " " + arg
except: # in case this is not unix except: # in case this is not unix
pass pass
self.files = [ arg ] self.files = [ arg ]

View File

@ -46,9 +46,9 @@ def compile(file, cfile=None, dfile=None):
import os, marshal, __builtin__ import os, marshal, __builtin__
f = open(file, 'U') f = open(file, 'U')
try: try:
timestamp = long(os.fstat(f.fileno())[8]) timestamp = long(os.fstat(f.fileno()).st_mtime)
except AttributeError: except AttributeError:
timestamp = long(os.stat(file)[8]) timestamp = long(os.stat(file).st_mtime)
codestring = f.read() codestring = f.read()
# If parsing from a string, line breaks are \n (see parsetok.c:tok_nextc) # If parsing from a string, line breaks are \n (see parsetok.c:tok_nextc)
# Replace will return original string if pattern is not found, so # Replace will return original string if pattern is not found, so

View File

@ -43,7 +43,7 @@ Mynd you, m
# the current directory is changed with os.chdir(), an incorrect # the current directory is changed with os.chdir(), an incorrect
# path will be displayed. # path will be displayed.
import sys, imp, os, stat, re, types, inspect import sys, imp, os, re, types, inspect
from repr import Repr from repr import Repr
from string import expandtabs, find, join, lower, split, strip, rfind, rstrip from string import expandtabs, find, join, lower, split, strip, rfind, rstrip
@ -153,7 +153,7 @@ def ispackage(path):
def synopsis(filename, cache={}): def synopsis(filename, cache={}):
"""Get the one-line summary out of a module file.""" """Get the one-line summary out of a module file."""
mtime = os.stat(filename)[stat.ST_MTIME] mtime = os.stat(filename).st_mtime
lastupdate, result = cache.get(filename, (0, None)) lastupdate, result = cache.get(filename, (0, None))
if lastupdate < mtime: if lastupdate < mtime:
info = inspect.getmoduleinfo(filename) info = inspect.getmoduleinfo(filename)
@ -1698,7 +1698,7 @@ class ModuleScanner(Scanner):
def __init__(self): def __init__(self):
roots = map(lambda dir: (dir, ''), pathdirs()) roots = map(lambda dir: (dir, ''), pathdirs())
Scanner.__init__(self, roots, self.submodules, self.isnewpackage) Scanner.__init__(self, roots, self.submodules, self.isnewpackage)
self.inodes = map(lambda (dir, pkg): os.stat(dir)[1], roots) self.inodes = map(lambda (dir, pkg): os.stat(dir).st_ino, roots)
def submodules(self, (dir, package)): def submodules(self, (dir, package)):
children = [] children = []
@ -1712,7 +1712,7 @@ class ModuleScanner(Scanner):
return children return children
def isnewpackage(self, (dir, package)): def isnewpackage(self, (dir, package)):
inode = os.path.exists(dir) and os.stat(dir)[1] inode = os.path.exists(dir) and os.stat(dir).st_ino
if not (os.path.islink(dir) and inode in self.inodes): if not (os.path.islink(dir) and inode in self.inodes):
self.inodes.append(inode) # detect circular symbolic links self.inodes.append(inode) # detect circular symbolic links
return ispackage(dir) return ispackage(dir)

View File

@ -79,4 +79,4 @@ def isdir(path):
st = stat(path) st = stat(path)
except _os.error: except _os.error:
return False return False
return S_ISDIR(st[ST_MODE]) return S_ISDIR(st.st_mode)

View File

@ -52,7 +52,7 @@ def encode(in_file, out_file, name=None, mode=None):
name = os.path.basename(in_file) name = os.path.basename(in_file)
if mode is None: if mode is None:
try: try:
mode = os.stat(in_file)[0] mode = os.stat(in_file).st_mode
except AttributeError: except AttributeError:
pass pass
in_file = open(in_file, 'rb') in_file = open(in_file, 'rb')

View File

@ -373,7 +373,7 @@ class ZipFile:
"""Put the bytes from filename into the archive under the name """Put the bytes from filename into the archive under the name
arcname.""" arcname."""
st = os.stat(filename) st = os.stat(filename)
mtime = time.localtime(st[8]) mtime = time.localtime(st.st_mtime)
date_time = mtime[0:6] date_time = mtime[0:6]
# Create ZipInfo instance to store file information # Create ZipInfo instance to store file information
if arcname is None: if arcname is None:
@ -572,10 +572,10 @@ class PyZipFile(ZipFile):
file_pyc = pathname + ".pyc" file_pyc = pathname + ".pyc"
file_pyo = pathname + ".pyo" file_pyo = pathname + ".pyo"
if os.path.isfile(file_pyo) and \ if os.path.isfile(file_pyo) and \
os.stat(file_pyo)[8] >= os.stat(file_py)[8]: os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:
fname = file_pyo # Use .pyo file fname = file_pyo # Use .pyo file
elif not os.path.isfile(file_pyc) or \ elif not os.path.isfile(file_pyc) or \
os.stat(file_pyc)[8] < os.stat(file_py)[8]: os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:
import py_compile import py_compile
if self.debug: if self.debug:
print "Compiling", file_py print "Compiling", file_py