Skip to content

gh-85681: Make ntpath.abspath() platform independent - #155395

Open
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-85681-abspath-fallback-strip
Open

gh-85681: Make ntpath.abspath() platform independent#155395
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-85681-abspath-fallback-strip

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

ntpath.relpath() returned different results on Windows and on other platforms:

>>> ntpath.relpath('foo ', 'foo')   # on Windows
'.'
>>> ntpath.relpath('foo ', 'foo')   # on other platforms
'..\\foo '

relpath() is based on abspath(), which is implemented with GetFullPathNameW() on Windows. It strips trailing dots and spaces from the last component of the path, and a single trailing dot from other components, unless it is preceded by a dot. The pure Python abspath(), used on other platforms, now does the same.

normpath() is not changed: it does not strip them on Windows either.

The rules are not documented, they were determined by testing nt._getfullpathname() on Windows 11. The added tests are run on all platforms, so they check both implementations.

On Windows the path normalization strips trailing dots and spaces from the
last component of the path, and a single trailing dot from other components.
abspath() on other platforms now does the same, so relpath() and other
functions based on it no longer depend on the platform.
@zooba

zooba commented Aug 18, 2026

Copy link
Copy Markdown
Member

Can we clearly mark the new function as "best effort to imitate Windows behaviour, but not a specification"?

I want to avoid getting stuck in the future having to use our own implementation (with all the quirks) everywhere because someone looks at it and decides that it's more important to have cross-platform consistency than it is to have cross-process/native consistency. I'd rather have weird edge cases be different between Windows/Linux than between Python and other native Windows apps.

Just a comment in the code will do, maybe in the tests as well.

@zooba

zooba commented Aug 18, 2026

Copy link
Copy Markdown
Member

With that, this PR looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants