python / python/cpython

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

Open
#138,139 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending stdlib
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the os.path.realpath behavior described in the examples, then inspect related path normalization such as normpath and pathlib.Path.parts. Confirm the intended POSIX treatment of exactly two leading slashes and whether behavior should depend on the platform. Done means the behavior is decided, implemented consistently where appropriate, and covered by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.