Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[3.13] gh-126780: Fix ntpath.normpath() for drive-relative paths (GH-126801) #127103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-126780: Fix ntpath.normpath() for drive-relative paths (GH-126801)
(cherry picked from commit 60ec854)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
  • Loading branch information
nineteendo authored and miss-islington committed Nov 21, 2024
commit f7671da9157e68acfb873f5a0c46523eb281b4c1
5 changes: 5 additions & 0 deletions 5 Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,18 @@ def test_normpath(self):

tester("ntpath.normpath('..')", r'..')
tester("ntpath.normpath('.')", r'.')
tester("ntpath.normpath('c:.')", 'c:')
tester("ntpath.normpath('')", r'.')
tester("ntpath.normpath('/')", '\\')
tester("ntpath.normpath('c:/')", 'c:\\')
tester("ntpath.normpath('/../.././..')", '\\')
tester("ntpath.normpath('c:/../../..')", 'c:\\')
tester("ntpath.normpath('/./a/b')", r'\a\b')
tester("ntpath.normpath('c:/./a/b')", r'c:\a\b')
tester("ntpath.normpath('../.././..')", r'..\..\..')
tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
tester("ntpath.normpath('./a/b')", r'a\b')
tester("ntpath.normpath('c:./a/b')", r'c:a\b')
tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')

Expand Down
2 changes: 2 additions & 0 deletions 2 Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ def test_expanduser_pwd2(self):
("/.", "/"),
("/./", "/"),
("/.//.", "/"),
("/./foo/bar", "/foo/bar"),
("/foo", "/foo"),
("/foo/bar", "/foo/bar"),
("//", "//"),
Expand All @@ -388,6 +389,7 @@ def test_expanduser_pwd2(self):
("///..//./foo/.//bar", "/foo/bar"),
(".", "."),
(".//.", "."),
("./foo/bar", "foo/bar"),
("..", ".."),
("../", ".."),
("../foo", "../foo"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`os.path.normpath` for drive-relative paths on Windows.
51 changes: 26 additions & 25 deletions 51 Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2506,37 +2506,38 @@ _Py_normpath_and_size(wchar_t *path, Py_ssize_t size, Py_ssize_t *normsize)
#endif
#define SEP_OR_END(x) (IS_SEP(x) || IS_END(x))

if (p1[0] == L'.' && IS_SEP(&p1[1])) {
// Skip leading '.\'
path = &path[2];
while (IS_SEP(path)) {
path++;
}
p1 = p2 = minP2 = path;
lastC = SEP;
}
else {
Py_ssize_t drvsize, rootsize;
_Py_skiproot(path, size, &drvsize, &rootsize);
if (drvsize || rootsize) {
// Skip past root and update minP2
p1 = &path[drvsize + rootsize];
Py_ssize_t drvsize, rootsize;
_Py_skiproot(path, size, &drvsize, &rootsize);
if (drvsize || rootsize) {
// Skip past root and update minP2
p1 = &path[drvsize + rootsize];
#ifndef ALTSEP
p2 = p1;
p2 = p1;
#else
for (; p2 < p1; ++p2) {
if (*p2 == ALTSEP) {
*p2 = SEP;
}
for (; p2 < p1; ++p2) {
if (*p2 == ALTSEP) {
*p2 = SEP;
}
}
#endif
minP2 = p2 - 1;
lastC = *minP2;
minP2 = p2 - 1;
lastC = *minP2;
#ifdef MS_WINDOWS
if (lastC != SEP) {
minP2++;
}
if (lastC != SEP) {
minP2++;
}
#endif
}
if (p1[0] == L'.' && SEP_OR_END(&p1[1])) {
// Skip leading '.\'
lastC = *++p1;
#ifdef ALTSEP
if (lastC == ALTSEP) {
lastC = SEP;
}
#endif
while (IS_SEP(p1)) {
p1++;
}
}

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.