Also compare filenames.

This commit is contained in:
Bastian Kleineidam 2012-05-11 21:04:02 +02:00
parent a690ba28f8
commit 26727ee20a
2 changed files with 13 additions and 1 deletions

View File

@ -404,9 +404,18 @@ def strlist_with_or (alist):
def is_same_file (filename1, filename2):
"""Check if filename1 and filename2 point to the same file object."""
"""Check if filename1 and filename2 point to the same file object.
There can be false negatives, ie. the result is False, but it is
the same file anyway. Reason is that network filesystems can create
different paths to the same physical file.
"""
if filename1 == filename2:
return True
if os.name == 'posix':
return os.path.samefile(filename1, filename2)
return is_same_filename(filename1, filename2)
def is_same_filename (filename1, filename2):
"""Check if filename1 and filename2 are the same filename."""
return os.path.realpath(filename1) == os.path.realpath(filename2)

View File

@ -22,12 +22,14 @@ class UtilTest (unittest.TestCase):
def test_samefile1 (self):
filename1 = filename2 = __file__
self.assertTrue(util.is_same_filename(filename1, filename2))
self.assertTrue(util.is_same_file(filename1, filename2))
def test_samefile2 (self):
parentdir = os.path.dirname(__file__)
filename1 = os.path.dirname(parentdir)
filename2 = os.path.join(parentdir, '..')
self.assertTrue(util.is_same_filename(filename1, filename2))
self.assertTrue(util.is_same_file(filename1, filename2))
def test_samefile3 (self):
@ -35,3 +37,4 @@ class UtilTest (unittest.TestCase):
filename1 = os.path.dirname(parentdir)
filename2 = os.path.join(parentdir, '.')
self.assertFalse(util.is_same_file(filename1, filename2))
self.assertFalse(util.is_same_filename(filename1, filename2))