`os.path.realpath` (and possibly others) ignore the special POSIX-meaning of absolute pathnames that start with exactly two `/`?
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
Hey.
As requested in #134639 moving the issue mentioned there to a separate bug.
POSIX defines that absolute pathnames that start with exactly (and only) two / are special:
If a pathname begins with two successive characters, the first component following the leading characters may be interpreted in an implementation-defined manner, although more than two leading characters shall be treated as a single character.
That is /foo, ///foo, ////foo, etc. are (normalised) all the same as /foo, but //foo is not necessarily (it's implementation-defined whether or not, so it actually may be and e.g. Linux indeed seems to consider it also the same, though I haven't verified whether it really is in all cases).
At least os.path.realpath seems to ignore that however:
>>> os.path.realpath("/foo")
'/foo'
>>> os.path.realpath("///foo")
'/foo'
>>> os.path.realpath("//foo")
'/foo'
while some Python library parts do in fact account for this, e.g.:
>>> pathlib.Path("//foo").parts
('//', 'foo')
>>> pathlib.Path("/foo").parts
('/', 'foo')
>>> pathlib.Path("///foo").parts
('/', 'foo')
I'm kinda tempted to say that os.path.realpath("//foo") should yield //foo.
Now to me, POSIX is not 100% clear (one should perhaps ask for confirmation at the Austin Group before making any incompatible changes):
I, personally, would interpret the above wording of the standard, that //foo/bar/baz merely indicates that foo (and foo alone) is special (e.g. like what we know from Windows with \\hostname\path) but any further components are not.
This could(would) in turn man that normalisation of any later components does happen as usual, and thus e.g. //foo/bar//baz should be normalised to //foo/bar/baz.
The leading // need of course be retained to indicate that foo is still special.
There is of course some "risk" in changing the current behaviour.
One could e.g. simply make it platform dependent, and e.g. on Linux leave things as is, and only change it for platforms (if any exist) that truly handle //foo special. One should do so only, if e.g. the POSIX/C realpath() would do so, too (on that platform).
But even then, other parts of Python do already handle // special... so in order to keep things homogeneous and compatible, one should perhaps anyway adapt os.path.realpath (and possibly others like .normpath().
Cheers,
Chris.
Thanks,
Chris.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、例で説明されている os.path.realpath の動作から始め、次に normpath や pathlib.Path.parts など、関連するパス正規化を調査します。先頭にスラッシュがちょうど2つある場合の意図された POSIX 上の扱いと、動作をプラットフォームに依存させるべきかどうかを確認します。動作が決定され、適切な箇所で一貫して実装され、回帰テストでカバーされれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- operating-systems
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100