python / python/cpython

`os.path.realpath` (and possibly others) ignore the special POSIX-meaning of absolute pathnames that start with exactly two `/`?

Đang mở
#138,139 7 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

pending stdlib
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với hành vi của os.path.realpath được mô tả trong các ví dụ, sau đó kiểm tra các cách chuẩn hóa đường dẫn liên quan như normpath và pathlib.Path.parts. Xác nhận cách xử lý POSIX dự kiến đối với chính xác hai dấu gạch chéo ở đầu và liệu hành vi có nên phụ thuộc vào nền tảng hay không. Công việc được xem là hoàn tất khi hành vi đã được quyết định, được triển khai nhất quán ở những nơi phù hợp và được bao phủ bằng các bài kiểm thử hồi quy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.