Also compare filenames.
This commit is contained in:
parent
a690ba28f8
commit
26727ee20a
|
@ -404,9 +404,18 @@ def strlist_with_or (alist):
|
||||||
|
|
||||||
|
|
||||||
def is_same_file (filename1, filename2):
|
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:
|
if filename1 == filename2:
|
||||||
return True
|
return True
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
return os.path.samefile(filename1, filename2)
|
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)
|
return os.path.realpath(filename1) == os.path.realpath(filename2)
|
||||||
|
|
|
@ -22,12 +22,14 @@ class UtilTest (unittest.TestCase):
|
||||||
|
|
||||||
def test_samefile1 (self):
|
def test_samefile1 (self):
|
||||||
filename1 = filename2 = __file__
|
filename1 = filename2 = __file__
|
||||||
|
self.assertTrue(util.is_same_filename(filename1, filename2))
|
||||||
self.assertTrue(util.is_same_file(filename1, filename2))
|
self.assertTrue(util.is_same_file(filename1, filename2))
|
||||||
|
|
||||||
def test_samefile2 (self):
|
def test_samefile2 (self):
|
||||||
parentdir = os.path.dirname(__file__)
|
parentdir = os.path.dirname(__file__)
|
||||||
filename1 = os.path.dirname(parentdir)
|
filename1 = os.path.dirname(parentdir)
|
||||||
filename2 = os.path.join(parentdir, '..')
|
filename2 = os.path.join(parentdir, '..')
|
||||||
|
self.assertTrue(util.is_same_filename(filename1, filename2))
|
||||||
self.assertTrue(util.is_same_file(filename1, filename2))
|
self.assertTrue(util.is_same_file(filename1, filename2))
|
||||||
|
|
||||||
def test_samefile3 (self):
|
def test_samefile3 (self):
|
||||||
|
@ -35,3 +37,4 @@ class UtilTest (unittest.TestCase):
|
||||||
filename1 = os.path.dirname(parentdir)
|
filename1 = os.path.dirname(parentdir)
|
||||||
filename2 = os.path.join(parentdir, '.')
|
filename2 = os.path.join(parentdir, '.')
|
||||||
self.assertFalse(util.is_same_file(filename1, filename2))
|
self.assertFalse(util.is_same_file(filename1, filename2))
|
||||||
|
self.assertFalse(util.is_same_filename(filename1, filename2))
|
||||||
|
|
Loading…
Reference in New Issue