Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ntpath.abspath() always return absolute path #119826

Closed
nineteendo opened this issue May 31, 2024 · 9 comments
Closed

ntpath.abspath() always return absolute path #119826

nineteendo opened this issue May 31, 2024 · 9 comments
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@nineteendo
Copy link
Contributor

nineteendo commented May 31, 2024

Feature or enhancement

Proposal:

ntpath.abspath() doesn't always return an absolute path:

>>> import ntpath
>>> ntpath.abspath('C:\x00')
'C:\x00' # instead of 'C:\\Users\\wanne\\\x00'
>>> ntpath.abspath('\x00:')
'\x00:' # instead of '\x00:\\'

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Linked PRs

@nineteendo nineteendo added the type-bug An unexpected behavior, bug, or error label May 31, 2024
@nineteendo
Copy link
Contributor Author

@erlend-aasland erlend-aasland added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 31, 2024
@nineteendo
Copy link
Contributor Author

cc @barneygale, @eryksun

@eryksun
Copy link
Contributor

eryksun commented May 31, 2024

I'm mostly concerned with fixing abspath() on Windows, by (1) using a private _path_normpath_ex() function that supports preserving a leading "." component, and (2) fixing the fallback implementation to correctly support drive-relative paths.

The case of normpath('C:.') is a bug in the C implementation that can be fixed. The pure Python implementation of ntpath.normpath() returns the correct result, "C:".

Note that no changes are required for the documented behavior and call signature of normpath() itself.

@barneygale, pathlib.PureWindowsPath was changed in 3.12+ to preserve an explicit leading "." in the case of relative paths that are ambiguous with drive-relative paths, such as ".\C:spam", but not generally for relative paths, such as ".\con". Would it possible and reasonable to make pathlib.PureWindowsPath always preserve an explicit initial ".", or maybe if there's only one subsequent component?

@barneygale
Copy link
Contributor

Would it possible and reasonable to make pathlib.PureWindowsPath always preserve an explicit initial ".", or maybe if there's only one subsequent component?

I think this is too likely to break users code if Path('foo') and Path('./foo') no longer hash/compare equal. There may be cases where users are relying on pathlib to remove that leading ./.

The dropping of leading ./ and trailing / is called out in the pathlib docs from 3.13: https://docs.python.org/3.13/library/pathlib.html#comparison-to-the-os-and-os-path-modules

I wish I could fix it :( but I can't see a route that won't cause unreasonable breakage. I wish we'd caught this while pathlib was still provisional.

@nineteendo
Copy link
Contributor Author

I split up the pull request to make it easier to review. Feel free to take a look if you have time.

@nineteendo nineteendo changed the title Improve accuracy of ntpath.normpath() & ntpath.abspath() ntpath.abspath() always return absolute path Nov 13, 2024
@picnixz picnixz added the stdlib Python modules in the Lib dir label Nov 14, 2024
@picnixz
Copy link
Contributor

picnixz commented Nov 14, 2024

The issue should probably be split up in 3:

  1. normpath('C:.'): bug for relative paths
  2. abspath('C:\x00'): always return absolute path
  3. abspath('./con'): support qualified referencing
    The third counts as a feature, but I'm not sure about the first two.

Originally posted by @nineteendo in #119938 (comment)

Let's address the above points to decide whether this one should be closed as completed or not.

cc @erlend-aasland

@nineteendo
Copy link
Contributor Author

nineteendo commented Nov 14, 2024

The issue has already been split up, so we should decide whether always returning an absolute path for abspath() is a bug fix or feature.

@nineteendo
Copy link
Contributor Author

@zooba, can this be closed?

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 2, 2024
…ythonGH-119938)

(cherry picked from commit 4b00aba)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 2, 2024
…ythonGH-119938)

(cherry picked from commit 4b00aba)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
@zooba zooba added type-bug An unexpected behavior, bug, or error 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes and removed type-feature A feature request or enhancement labels Dec 2, 2024
zooba pushed a commit that referenced this issue Dec 2, 2024
(cherry picked from commit 4b00aba)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
zooba pushed a commit that referenced this issue Dec 2, 2024
(cherry picked from commit 4b00aba)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
@nineteendo
Copy link
Contributor Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants