mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
By setting the st_dev attribute, we can then remove some Windows-specific code and move os.path.samefile/sameopenfile/samestat to Lib/genericpath.py so all platforms share the same implementation.
This commit is contained in:
parent
2bf61abe02
commit
490b32a397
7 changed files with 104 additions and 139 deletions
|
@ -190,6 +190,74 @@ class GenericTest(unittest.TestCase):
|
|||
support.unlink(support.TESTFN)
|
||||
safe_rmdir(support.TESTFN)
|
||||
|
||||
@staticmethod
|
||||
def _create_file(filename):
|
||||
with open(filename, 'wb') as f:
|
||||
f.write(b'foo')
|
||||
|
||||
def test_samefile(self):
|
||||
try:
|
||||
test_fn = support.TESTFN + "1"
|
||||
self._create_file(test_fn)
|
||||
self.assertTrue(self.pathmodule.samefile(test_fn, test_fn))
|
||||
self.assertRaises(TypeError, self.pathmodule.samefile)
|
||||
finally:
|
||||
os.remove(test_fn)
|
||||
|
||||
@support.skip_unless_symlink
|
||||
def test_samefile_on_links(self):
|
||||
try:
|
||||
test_fn1 = support.TESTFN + "1"
|
||||
test_fn2 = support.TESTFN + "2"
|
||||
self._create_file(test_fn1)
|
||||
|
||||
os.symlink(test_fn1, test_fn2)
|
||||
self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
|
||||
os.remove(test_fn2)
|
||||
|
||||
self._create_file(test_fn2)
|
||||
self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
|
||||
finally:
|
||||
os.remove(test_fn1)
|
||||
os.remove(test_fn2)
|
||||
|
||||
def test_samestat(self):
|
||||
try:
|
||||
test_fn = support.TESTFN + "1"
|
||||
self._create_file(test_fn)
|
||||
test_fns = [test_fn]*2
|
||||
stats = map(os.stat, test_fns)
|
||||
self.assertTrue(self.pathmodule.samestat(*stats))
|
||||
finally:
|
||||
os.remove(test_fn)
|
||||
|
||||
@support.skip_unless_symlink
|
||||
def test_samestat_on_links(self):
|
||||
try:
|
||||
test_fn1 = support.TESTFN + "1"
|
||||
test_fn2 = support.TESTFN + "2"
|
||||
self._create_file(test_fn1)
|
||||
test_fns = (test_fn1, test_fn2)
|
||||
os.symlink(*test_fns)
|
||||
stats = map(os.stat, test_fns)
|
||||
self.assertTrue(self.pathmodule.samestat(*stats))
|
||||
os.remove(test_fn2)
|
||||
|
||||
self._create_file(test_fn2)
|
||||
stats = map(os.stat, test_fns)
|
||||
self.assertFalse(self.pathmodule.samestat(*stats))
|
||||
|
||||
self.assertRaises(TypeError, self.pathmodule.samestat)
|
||||
finally:
|
||||
os.remove(test_fn1)
|
||||
os.remove(test_fn2)
|
||||
|
||||
def test_sameopenfile(self):
|
||||
fname = support.TESTFN + "1"
|
||||
with open(fname, "wb") as a, open(fname, "wb") as b:
|
||||
self.assertTrue(self.pathmodule.sameopenfile(
|
||||
a.fileno(), b.fileno()))
|
||||
|
||||
|
||||
# Following TestCase is not supposed to be run from test_genericpath.
|
||||
# It is inherited by other test modules (macpath, ntpath, posixpath).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue