Result of os.path.realpath("") is incorrect if cwd is on letter-less drive (\\?\-prefixed)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 36k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
- Create volume, dir on it, don't assign a drive letter
- In Python:
>>> import os
>>> os.chdir("\\\\?\\Volume{<uuid>}\\<dir>")
>>> os.path.realpath("")
'\\\\?\\Volume{<uuid>}\\<dir>\\.'
The dot is invalid in \\?\-prefixed path.
Dots (. and ..) are normalized by Windows API (at least on NTFS). But \\?\ paths bypass normalization and so may not contain dots. See, for example, here.
All later WinAPI calls with this path fail.
As a simple test, dir \\?\Volume{<uuid>}\dir\. doesn't work whereas dir \\?\Volume{<uuid>}\dir works.
P.S. os.path.realpath(".") gives the same invalid result
Your environment
CPython 3.10.0
Windows 10
Context
The problem arises from pip.
sys.path contains empty string (meaning current dir I presume).
And legacy installation method resolves sys.path relatively to cwd.
Investigation
Here:
https://github.com/python/cpython/blob/4bed0db7c222f8df1b4e31107c0305214caf3f56/Lib/ntpath.py#L704
os.path.join still adds slash even if the second argument is empty. And if the path is ended with slash, dot is added somewhere below.
I fixed it to:
path = cwd if path == "." or path == "" else join(cwd, path)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne in Lib/ntpath.py bei der referenzierten Zeile 704 und bilde das Windows-Verhalten mit einem \?-präfixierten Volume-Pfad nach, dessen aktuelles Verzeichnis keinen Laufwerksbuchstaben hat. Prüfe, wie os.path.realpath("") und os.path.realpath(".") ihre Pfade konstruieren, und füge dann Regressionstestabdeckung hinzu, die zeigt, dass keines der beiden Ergebnisse mit einer ungültigen \. Komponente endet und dass der resultierende Pfad mit späteren WinAPI-Aufrufen funktioniert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- operating-systems
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100