Skip to content

ntpath.abspath() always return absolute path #119826

Description

@nineteendo

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

Activity

  1. nineteendo commented on May 31, 2024

    @nineteendo
    ContributorAuthor
  2. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on May 31, 2024
  3. nineteendo commented on May 31, 2024

    @nineteendo
    ContributorAuthor
  4. eryksun commented on May 31, 2024

    @eryksun
    Contributor

    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?

  5. barneygale commented on May 31, 2024

    @barneygale
    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.

  6. nineteendo commented on Jun 2, 2024

    @nineteendo
    ContributorAuthor

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

  7. added a commit that references this issue on Nov 12, 2024
  8. changed the title [-]Improve accuracy of `ntpath.normpath()` & `ntpath.abspath()`[/-] [+]`ntpath.abspath()` always return absolute path[/+] on Nov 13, 2024
  9. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 14, 2024
  10. picnixz commented on Nov 14, 2024

    @picnixz
    Member

    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

  11. nineteendo commented on Nov 14, 2024

    @nineteendo
    ContributorAuthor

    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.

  12. nineteendo commented on Nov 21, 2024

    @nineteendo
    ContributorAuthor

    @zooba, can this be closed?

  13. added 2 commits that reference this issue on Dec 2, 2024
  14. added
    type-bugAn unexpected behavior, bug, or error
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    and removed
    type-featureA feature request or enhancement
    on Dec 2, 2024
  15. added 2 commits that reference this issue on Dec 2, 2024
  16. nineteendo commented on Dec 3, 2024

    @nineteendo
    ContributorAuthor

    Thanks.

  17. added a commit that references this issue on Dec 8, 2024
  18. added a commit that references this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions