Skip to content

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

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:gh-85681-abspath-fallback-strip
Nov 24, 2026
Merged

serhiy-storchaka merged 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.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Nov 17, 2026
@serhiy-storchaka
serhiy-storchaka merged commit 518af64 into python:main Nov 24, 2026
51 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the gh-85681-abspath-fallback-strip branch November 24, 2026 15:23
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Thank you for review @zooba.

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

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants