[issue43105] [Windows] Can't import extension modules resolved via relative paths in sys.path
Eryk Sun
report at bugs.python.org
Wed Apr 7 17:13:43 EDT 2021
Eryk Sun <eryksun at gmail.com> added the comment:
> The 3.8 backport is much more complicated, as we don't have access to
> the PathSkipRoot function there. So we can't use the native function.
I guess you missed the comment that I left on the PR a few days ago. The 3.8 backport can use the older PathSkipRootW() in shlwapi.dll [1]. It works similarly, except it doesn't support a drive relative root such as "C:spam" -> ("C:", "spam"), but it's easy enough to handle that case in C. Also, it's documented that it's limited to MAX_PATH, but it works fine with long paths in Windows 10, even if the process does not have long-path support enabled. Anyway, just limit the copy to the first `MAX_PATH - 1` characters. Practically speaking, no root is anywhere near that long. It would require a ridiculously long device name in a "\\.\" device path.
Examples:
import ctypes
shlwapi = ctypes.WinDLL('shlwapi')
shlwapi.PathSkipRootW.restype = ctypes.c_wchar_p
path = (ctypes.c_wchar * 1000)()
It returns NULL if there's no root:
>>> path.value = r'spam'
>>> shlwapi.PathSkipRootW(path) is None
True
Drive-relative paths aren't supported the same as they are in PathCchSkipRoot():
>>> path.value = r'C:spam'
>>> shlwapi.PathSkipRootW(path) is None
True
Otherwise it seems to support the same range of paths as PathCchSkipRoot():
>>> path.value = r'\spam'
>>> shlwapi.PathSkipRootW(path)
'spam'
>>> path.value = r'C:\spam'
>>> shlwapi.PathSkipRootW(path)
'spam'
>>> path.value = r'\\server\share\spam'
>>> shlwapi.PathSkipRootW(path)
'spam'
>>> path.value = r'\\?\C:\spam'
>>> shlwapi.PathSkipRootW(path)
'spam'
>>> path.value = r'\\?\UNC\server\share\spam'
>>> shlwapi.PathSkipRootW(path)
'spam'
>>> path.value = r'\\?\Volume{12345678-1234-1234-1234-123456789ABC}\spam'
>>> shlwapi.PathSkipRootW(path)
'spam'
>>> path.value = r'\\.\PIPE\spam'
>>> shlwapi.PathSkipRootW(path)
'spam'
---
[1] https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathskiprootw
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43105>
_______________________________________
More information about the Python-bugs-list
mailing list