GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-99029: Fix handling of `PureWindowsPath('C:\<blah>').relative_to('C:')` by barneygale · Pull Request #99031 · python/cpython

Conversation

@barneygale

@barneygale barneygale commented Nov 2, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

relative_to() now treats naked drive paths as relative. This brings its behaviour in line with other parts of pathlib, and with ntpath.relpath(), and so allows us to factor out the pathlib-specific implementation.

  • Issue: Disagreement between PureWindowsPath.relative_to() and ntpath.relpath() with rootless drives #99029

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@barneygale
pythongh-99029: Fix handling of `PureWindowsPath('C:\<blah>').relativ…
bdfcc65
…e_to('C:')`

`relative_to()` now treats naked drive paths as relative. This brings its
behaviour in line with other parts of pathlib, and with `ntpath.relpath()`,
and so allows us to factor out the pathlib-specific implementation.
@barneygale
barneygale requested a review from brettcannon as a code owner November 2, 2022 23:48
@bedevere-bot bedevere-bot mentioned this pull request Nov 2, 2022
Disagreement between PureWindowsPath.relative_to() and ntpath.relpath() with rootless drives #99029
Closed
@bedevere-bot bedevere-bot added the awaiting review label Nov 2, 2022
This was referenced Nov 2, 2022
gh-68320, gh-88302 - Allow for pathlib.Path subclassing #31691
Merged
gh-78707: deprecate passing >1 argument to PurePath.[is_]relative_to() #94469
Merged
gh-84538: add strict argument to pathlib.PurePath.relative_to #19813
Merged
@domragusa

domragusa commented Nov 3, 2022

Copy link
Copy Markdown
Contributor

The implementation of os.path.relpath accesses the filesystem making the two paths absolute and then calculating the common path, using that function to implement a PurePath method is wrong becase it should work without the any file or directory being there (its definition is "Base class for manipulating paths without I/O"), the use case that it breaks is when we don't have files on disk for example I use it to create the structure of a zip archive.

Removing tests is not a good idea.

Mixing relative and absolute paths should raise an exception as you stated and should be fixed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Nov 3, 2022

Copy link
Copy Markdown
Contributor Author

I didn't remove any tests. Please share an example of where this breaks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Nov 3, 2022

Copy link
Copy Markdown
Contributor Author

I told a lie - I actually did remove two assertions that supply strings to the method. Will fix!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Restore deleted assertions
4b190a1
@domragusa

domragusa commented Nov 3, 2022

Copy link
Copy Markdown
Contributor

I thought that os.path.relpath accessed the filesystem, but taking a better look at it I guess it doesn't.

I don't have a Windows machine and I can't provide a concrete example, I'm sorry, anyway it calls os.path.abspath and following the chain we arrive at a Windows api call GetFullPathNameW, that's where I expect PurePath to break because it should be platform independent and this change would make it depend on the result of a winapi call.
Can you test it with names and characters that are reserved on Windows but are allowed on Posix-systems?

I would have just tweaked the fail condition to make sure that we raise the exception for the bug you noticed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Nov 3, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

👍 thanks, I'll try that out!

It's true that relpath() can call os.getcwd(). However, the working directory only contributes to the result if we pass one relative path and one absolute. And that's specifically disallowed in pathlib's relative_to(). In other cases, the two matching prefixes cancel eachother out in commonpath().

The fact that relpath() needlessly calls abspath() when given two relative paths is regrettable, and probably worth fixing. But it shouldn't have an observable effect here. I'll try to find a counterexample.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@domragusa

domragusa commented Nov 3, 2022

Copy link
Copy Markdown
Contributor

It's not just getcwd, for example "c:" is a valid filename on Linux: on Windows after abspath you would get the current directory on the C drive while on Linux it's the file named "c:" in the current working directory so the calculations that follow would be wrong, there are also reserved names on Windows like COM or CON and characters that are not allowed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Nov 3, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I don't think that matters - os.getcwd() can return something totally nonsensical and we wouldn't care, as long as it returns the same thing twice in a row, so that the shared prefix can be eliminated. Right?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@domragusa

domragusa commented Nov 3, 2022

Copy link
Copy Markdown
Contributor

You're referencing os.getcwd() because you're looking at the implementation of os.path.abspath in os.posixpath but on Windows os.path resolves to os.ntpath and in that module os.path.abspath calls _getfullpathname() that calls the windows api.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Nov 3, 2022

Copy link
Copy Markdown
Contributor Author

Again, I don't think it matters. If you think it does, could you please provide a reproduction case. Thank you.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Nov 7, 2022

Copy link
Copy Markdown
Contributor Author

I've logged #99199 to cover relpath() needlessly calling abspath() when the paths' anchors match.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Stop using os.path.relpath()
4d13ac8
Differences in handling of '..' parts still make these functions
fundamentally incompatible.
@barneygale

barneygale commented Nov 12, 2022

Copy link
Copy Markdown
Contributor Author

Turns out there's another incompatibility related to leading ../ segments - see #99199 (comment)

I've revised the implementation to not use relpath() :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Simplify algorithm a little
3b37a0d
@barneygale barneygale mentioned this pull request Nov 13, 2022
Clarify the documentation of pathlib.Path.is_relative_to() #99334
Closed
brettcannon
brettcannon requested changes Nov 18, 2022
View reviewed changes

@brettcannon brettcannon left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I believe this is in line with the result of the discussion in the issue, so only last change is to use f-strings!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting changes and removed awaiting review labels Nov 18, 2022
@bedevere-bot

bedevere-bot commented Nov 18, 2022

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Use f-strings for exception messages
a3e5517
@barneygale

barneygale commented Nov 19, 2022

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Nov 19, 2022
@bedevere-bot

bedevere-bot commented Nov 19, 2022

Copy link
Copy Markdown

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot
bedevere-bot requested a review from brettcannon November 19, 2022 01:12
brettcannon
brettcannon approved these changes Nov 25, 2022
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Nov 25, 2022
@brettcannon
brettcannon merged commit ae234fb into python:main Nov 25, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Nov 25, 2022
@brettcannon

brettcannon commented Nov 25, 2022

Copy link
Copy Markdown
Member

Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Nov 25, 2022

Copy link
Copy Markdown
Contributor Author

Thank you!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lazka lazka mentioned this pull request Nov 19, 2024
external_project: workaround for MinGW mesonbuild/meson#13886
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@brettcannon brettcannon brettcannon approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@barneygale @domragusa @bedevere-bot @brettcannon
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.