bpo-45506: Normalize _PyPathConfig.stdlib_dir when calculated. (#29040)

The recently added PyConfig.stdlib_dir was being set with ".." entries. When __file__ was added for from modules this caused a problem on out-of-tree builds. This PR fixes that by normalizing "stdlib_dir" when it is calculated in getpath.c.

https://bugs.python.org/issue45506
This commit is contained in:
Eric Snow 2021-10-22 17:20:03 -06:00 committed by GitHub
parent f30ad65dbf
commit 17c61045c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 236 additions and 24 deletions

View file

@ -304,25 +304,51 @@ class PosixPathTest(unittest.TestCase):
for path in ('~', '~/.local', '~vstinner/'):
self.assertEqual(posixpath.expanduser(path), path)
def test_normpath(self):
self.assertEqual(posixpath.normpath(""), ".")
self.assertEqual(posixpath.normpath("/"), "/")
self.assertEqual(posixpath.normpath("//"), "//")
self.assertEqual(posixpath.normpath("///"), "/")
self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"),
"/foo/baz")
self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
NORMPATH_CASES = [
("", "."),
("/", "/"),
("/.", "/"),
("/./", "/"),
("/.//.", "/"),
("/foo", "/foo"),
("/foo/bar", "/foo/bar"),
("//", "//"),
("///", "/"),
("///foo/.//bar//", "/foo/bar"),
("///foo/.//bar//.//..//.//baz///", "/foo/baz"),
("///..//./foo/.//bar", "/foo/bar"),
(".", "."),
(".//.", "."),
("..", ".."),
("../", ".."),
("../foo", "../foo"),
("../../foo", "../../foo"),
("../foo/../bar", "../bar"),
("../../foo/../bar/./baz/boom/..", "../../bar/baz"),
("/..", "/"),
("/..", "/"),
("/../", "/"),
("/..//", "/"),
("//..", "//"),
("/../foo", "/foo"),
("/../../foo", "/foo"),
("/../foo/../", "/"),
("/../foo/../bar", "/bar"),
("/../../foo/../bar/./baz/boom/..", "/bar/baz"),
("/../../foo/../bar/./baz/boom/.", "/bar/baz/boom"),
]
self.assertEqual(posixpath.normpath(b""), b".")
self.assertEqual(posixpath.normpath(b"/"), b"/")
self.assertEqual(posixpath.normpath(b"//"), b"//")
self.assertEqual(posixpath.normpath(b"///"), b"/")
self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar")
self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"),
b"/foo/baz")
self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"),
b"/foo/bar")
def test_normpath(self):
for path, expected in self.NORMPATH_CASES:
with self.subTest(path):
result = posixpath.normpath(path)
self.assertEqual(result, expected)
path = path.encode('utf-8')
expected = expected.encode('utf-8')
with self.subTest(path, type=bytes):
result = posixpath.normpath(path)
self.assertEqual(result, expected)
@skip_if_ABSTFN_contains_backslash
def test_realpath_curdir(self):