Issue #26027, #27524: Add PEP 519/__fspath__() support to os and

os.path.

Thanks to Jelle Zijlstra for the initial patch against posixmodule.c.
This commit is contained in:
Brett Cannon 2016-08-26 14:44:48 -07:00
parent 6ed442c48d
commit 3f9183b5ac
11 changed files with 424 additions and 52 deletions

View file

@ -596,5 +596,85 @@ class PosixCommonTest(test_genericpath.CommonTest, unittest.TestCase):
attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
class PathLikeTests(unittest.TestCase):
path = posixpath
class PathLike:
def __init__(self, path=''):
self.path = path
def __fspath__(self):
if isinstance(self.path, BaseException):
raise self.path
else:
return self.path
def setUp(self):
self.file_name = support.TESTFN.lower()
self.file_path = self.PathLike(support.TESTFN)
self.addCleanup(support.unlink, self.file_name)
with open(self.file_name, 'xb', 0) as file:
file.write(b"test_posixpath.PathLikeTests")
def assertPathEqual(self, func):
self.assertEqual(func(self.file_path), func(self.file_name))
def test_path_normcase(self):
self.assertPathEqual(self.path.normcase)
def test_path_isabs(self):
self.assertPathEqual(self.path.isabs)
def test_path_join(self):
self.assertEqual(self.path.join('a', self.PathLike('b'), 'c'),
self.path.join('a', 'b', 'c'))
def test_path_split(self):
self.assertPathEqual(self.path.split)
def test_path_splitext(self):
self.assertPathEqual(self.path.splitext)
def test_path_splitdrive(self):
self.assertPathEqual(self.path.splitdrive)
def test_path_basename(self):
self.assertPathEqual(self.path.basename)
def test_path_dirname(self):
self.assertPathEqual(self.path.dirname)
def test_path_islink(self):
self.assertPathEqual(self.path.islink)
def test_path_lexists(self):
self.assertPathEqual(self.path.lexists)
def test_path_ismount(self):
self.assertPathEqual(self.path.ismount)
def test_path_expanduser(self):
self.assertPathEqual(self.path.expanduser)
def test_path_expandvars(self):
self.assertPathEqual(self.path.expandvars)
def test_path_normpath(self):
self.assertPathEqual(self.path.normpath)
def test_path_abspath(self):
self.assertPathEqual(self.path.abspath)
def test_path_realpath(self):
self.assertPathEqual(self.path.realpath)
def test_path_relpath(self):
self.assertPathEqual(self.path.relpath)
def test_path_commonpath(self):
common_path = self.path.commonpath([self.file_path, self.file_name])
self.assertEqual(common_path, self.file_name)
if __name__=="__main__":
unittest.main()