Skip to content

Commit ab8e389

Browse files
committed
pythongh-12933: fix import error over nfs on Windows
On Windows, realpath uses `GetFinalPathNameByHandleW` to resolve paths. This returns paths starting with a `\\?\` prefix, which is usually not what we want. This change attempts to remove the prefix for \\?\UNC\ paths. A similar thing was already done for drive paths such as \\?\C:\.
1 parent d89a5f6 commit ab8e389

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ Erik Demaine
438438
Jeroen Demeyer
439439
Martin Dengler
440440
John Dennis
441+
Chris Denton
441442
L. Peter Deutsch
442443
Roger Dev
443444
Philippe Devalkeneer

Modules/getpath.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,24 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
540540
PyErr_SetFromWindowsErr(err);
541541
result = NULL;
542542
} else if (len <= MAXPATHLEN) {
543-
const wchar_t *p = resolved;
543+
wchar_t *p = resolved;
544544
if (0 == wcsncmp(p, L"\\\\?\\", 4)) {
545-
if (GetFileAttributesW(&p[4]) != INVALID_FILE_ATTRIBUTES) {
546-
p += 4;
547-
len -= 4;
545+
if (0 == wcsncmp(&p[4], L"UNC\\", 4)) {
546+
// A \\?\UNC\ path. Try converting to a \\ path.
547+
p[6] = L'\\';
548+
if (GetFileAttributesW(&p[6]) != INVALID_FILE_ATTRIBUTES) {
549+
p += 6;
550+
len -= 6;
551+
} else {
552+
// Change back to a \\?\UNC\ path.
553+
p[6] = L'C';
554+
}
555+
} else {
556+
// Maybe a drive path like \\?\C:\. Try stripping the prefix.
557+
if (GetFileAttributesW(&p[4]) != INVALID_FILE_ATTRIBUTES) {
558+
p += 4;
559+
len -= 4;
560+
}
548561
}
549562
}
550563
if (CompareStringOrdinal(path, (int)pathlen, p, len, TRUE) == CSTR_EQUAL) {

0 commit comments

Comments
 (0)