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

bpo-29688: document and test pathlib.Path.absolute(). #26153

Merged

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented May 15, 2021

This method is important and cannot be replaced with resolve()! resolve() will hit OS APIs to resolve symlinks/etc, and will fail on symlink loops, whereas absolute() just prepends the working directory and can never raise an OSError.

Also improved the table showing correspondence with os.path methods. The gory details:

  • Path.resolve() is os.path.realpath() in strict mode, with a tweaked exception type for symlink loops (backwards compat)
  • Path.absolute() is os.path.abspath() but without normalizing the path. The pathlib behaviour is better, as abspath()'s normlisation changes the meaning of the path in the presence of symlinks!

Happy to expand the tests if anyone has any ideas what else to cover.

https://bugs.python.org/issue29688

@kalvdans
Copy link

Documentation is clear and the suggested changes looks sane to me. Will take another look after windows test failures are addressed.

@barneygale
Copy link
Contributor Author

Tested are fixed, thanks for taking a look.

Copy link

@kalvdans kalvdans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small remarks.

Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
@barneygale
Copy link
Contributor Author

barneygale commented May 16, 2021

Note that this can't be automatically backported, as the tests' patching of getcwd() is different in 3.9 and below.

Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@github-actions
Copy link

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

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jul 15, 2021
Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One clarifying doc change as suggested by @domdfcoding. A news entry is also missing.

Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Jul 17, 2021
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@barneygale
Copy link
Contributor Author

Sorry for the long delay. Does this need a NEWS entry? It adds docs and tests but doesn't change the implementation of Path.absolute()

@barneygale barneygale force-pushed the bpo-29688-pathlib-absolute-docs-tests branch from 644703a to 64204fd Compare January 1, 2022 01:21
@barneygale barneygale requested a review from brettcannon January 1, 2022 03:06
@brettcannon
Copy link
Member

Sorry for the long delay. Does this need a NEWS entry? It adds docs and tests but doesn't change the implementation of Path.absolute()

It looks like you added a news entry anyway. 😄

Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about the docs and some very minor formatting things.

Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Lib/test/test_pathlib.py Outdated Show resolved Hide resolved
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
Comment on lines +2662 to +2670
self.assertEqual(str(P('c:\\').absolute()), 'c:\\')
self.assertEqual(str(P('c:\\a').absolute()), 'c:\\a')
self.assertEqual(str(P('c:\\a\\b').absolute()), 'c:\\a\\b')

# UNC absolute paths.
share = '\\\\server\\share\\'
self.assertEqual(str(P(share).absolute()), share)
self.assertEqual(str(P(share + 'a').absolute()), share + 'a')
self.assertEqual(str(P(share + 'a\\b').absolute()), share + 'a\\b')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.assertEqual(str(P('c:\\').absolute()), 'c:\\')
self.assertEqual(str(P('c:\\a').absolute()), 'c:\\a')
self.assertEqual(str(P('c:\\a\\b').absolute()), 'c:\\a\\b')
# UNC absolute paths.
share = '\\\\server\\share\\'
self.assertEqual(str(P(share).absolute()), share)
self.assertEqual(str(P(share + 'a').absolute()), share + 'a')
self.assertEqual(str(P(share + 'a\\b').absolute()), share + 'a\\b')
self.assertEqual(str(P(r'c:\').absolute()), r'c:\')
self.assertEqual(str(P(r'c:\a').absolute()), r'c:\a')
self.assertEqual(str(P(r'c:\a\b').absolute()), r'c:\a\b')
# UNC absolute paths.
share = '\\\\server\\share\\'
self.assertEqual(str(P(share).absolute()), share)
self.assertEqual(str(P(share + 'a').absolute()), share + 'a')
self.assertEqual(str(P(share + r'a\b').absolute()), share + r'a\b')

@brettcannon
Copy link
Member

Thanks, @barneygale !

@barneygale
Copy link
Contributor Author

Thanks very much for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants