Result of os.path.realpath("") is incorrect if cwd is on letter-less drive (\\?\-prefixed)

オープン
#94,696 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python

調査の方向性

Lib/ntpath.py の参照されている 704 行付近から始め、カレントディレクトリにドライブ文字がない \?-接頭辞付きのボリュームパスを使って Windows の動作を再現します。os.path.realpath("") と os.path.realpath(".") がどのようにパスを構築するかを確認し、その後、どちらの結果も無効な \. コンポーネントで終わらず、結果のパスが後続の WinAPI 呼び出しで機能することを示す回帰テストカバレッジを追加します。

索引モデルが issue の本文から書いたものです。

説明

OS-windows stdlib type-bug

Bug report

  1. Create volume, dir on it, don't assign a drive letter
  2. 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)
主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。