summaryrefslogtreecommitdiff
path: root/Lib/genericpath.py
diff options
context:
space:
mode:
authorWalter Doerwald <walter@livinglogic.de>2013-12-02 11:43:20 +0100
committerWalter Doerwald <walter@livinglogic.de>2013-12-02 11:43:20 +0100
commit1a7a32b4875516d530b78f524e83aefac9cad1ab (patch)
tree49024aa50be776f7233b7a7b3083549f258822dd /Lib/genericpath.py
parent97261caa90b6e16b6866de47febd059f3b0cd4a1 (diff)
parentd2fb1d098e418d099d7a2b7893a1201f606122fe (diff)
downloadcpython-1a7a32b4875516d530b78f524e83aefac9cad1ab.tar.gz
Fix #19834: merge with 3.3.
Diffstat (limited to 'Lib/genericpath.py')
-rw-r--r--Lib/genericpath.py34
1 files changed, 30 insertions, 4 deletions
diff --git a/Lib/genericpath.py b/Lib/genericpath.py
index 340c00494d..ca4a5108fd 100644
--- a/Lib/genericpath.py
+++ b/Lib/genericpath.py
@@ -7,7 +7,8 @@ import os
import stat
__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
- 'getsize', 'isdir', 'isfile']
+ 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile',
+ 'samestat']
# Does a path exist?
@@ -16,7 +17,7 @@ def exists(path):
"""Test whether a path exists. Returns False for broken symbolic links"""
try:
os.stat(path)
- except os.error:
+ except OSError:
return False
return True
@@ -27,7 +28,7 @@ def isfile(path):
"""Test whether a path is a regular file"""
try:
st = os.stat(path)
- except os.error:
+ except OSError:
return False
return stat.S_ISREG(st.st_mode)
@@ -39,7 +40,7 @@ def isdir(s):
"""Return true if the pathname refers to an existing directory."""
try:
st = os.stat(s)
- except os.error:
+ except OSError:
return False
return stat.S_ISDIR(st.st_mode)
@@ -75,6 +76,31 @@ def commonprefix(m):
return s1[:i]
return s1
+# Are two stat buffers (obtained from stat, fstat or lstat)
+# describing the same file?
+def samestat(s1, s2):
+ """Test whether two stat buffers reference the same file"""
+ return (s1.st_ino == s2.st_ino and
+ s1.st_dev == s2.st_dev)
+
+
+# Are two filenames really pointing to the same file?
+def samefile(f1, f2):
+ """Test whether two pathnames reference the same actual file"""
+ s1 = os.stat(f1)
+ s2 = os.stat(f2)
+ return samestat(s1, s2)
+
+
+# Are two open files really referencing the same file?
+# (Not necessarily the same file descriptor!)
+def sameopenfile(fp1, fp2):
+ """Test whether two open file objects reference the same file"""
+ s1 = os.fstat(fp1)
+ s2 = os.fstat(fp2)
+ return samestat(s1, s2)
+
+
# Split a path in root and extension.
# The extension is everything starting at the last dot in the last
# pathname component; the root is everything before that.