`os.path.realpath` (and possibly others) ignore the special POSIX-meaning of absolute pathnames that start with exactly two `/`?
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 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 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先從範例中描述的 os.path.realpath 行為開始,然後檢查相關的路徑正規化,例如 normpath 和 pathlib.Path.parts。確認對於恰好兩個前導斜線的預期 POSIX 處理方式,以及行為是否應取決於平台。行為確定下來、在適當的地方一致實作,並由回歸測試涵蓋後,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- operating-systems
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100