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 — bpo-38671: Make sure to return an absolute path in pathlib._WindowsFlavour.resolve() by uranusjr · Pull Request #17716 · python/cpython

bpo-38671: Make sure to return an absolute path in pathlib._WindowsFlavour.resolve() #17716

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.

Sign up for GitHub

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

Closed
uranusjr wants to merge 5 commits into python:master from uranusjr:path-resolve-nonstrict-win

Uh oh!

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

Closed

bpo-38671: Make sure to return an absolute path in pathlib._WindowsFlavour.resolve() #17716

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
e503cd6
Prepend cwd if resolve can't find existing prefix
uranusjr Dec 27, 2019
3f9d1b8
Use os.path.abspath() as fallback as suggested
uranusjr Jul 15, 2020
612b7f6
Use early returns to reduce indentation level
uranusjr Jul 15, 2020
7cff82e
Test non-exist resolve on all platforms
uranusjr Jul 19, 2020
8f20c10
Use nt._getfullpathname instead of os.path.abspath
uranusjr Jul 19, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

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

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

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

Diff view
Diff view

There are no files selected for viewing

38 changes: 19 additions & 19 deletions 38 Lib/pathlib.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


if os.name == 'nt':
from nt import _getfinalpathname
from nt import _getfinalpathname, _getfullpathname
else:
_getfinalpathname = None

Expand Down Expand Up @@ -189,26 +189,26 @@ def compile_pattern(self, pattern):
def resolve(self, path, strict=False):
s = str(path)
if not s:
return path._accessor.getcwd()
s = path._accessor.getcwd()
if _getfinalpathname is None:
return None # Means fallback on absolute
Comment thread
uranusjr marked this conversation as resolved.
Resolved

Uh oh!

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

if strict:
Comment thread Outdated

@eryksun eryksun Oct 5, 2020

Copy link
Copy Markdown
Contributor

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.

Lines 189-191 make a bad assumption that the current working directory (CWD) is a final path. The CWD is an absolute path but not a final path. The assignment should be something like s = str(path) or '.'. In practice it may never be an issue since PurePath.__str__ assigns and returns self._str = self._format_parsed_parts(self._drv, self._root, self._parts) or '.'.

Sorry, something went wrong.

Uh oh!

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

All reactions

@uranusjr uranusjr Oct 5, 2020

Copy link
Copy Markdown
Contributor Author

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.

The CWD is an absolute path but not a final path.

TIL.

I like the or '.' approach since it avoids special-casing things, but want @pitrou to take a look since this is inherited from the initial pathlib implementation.

Sorry, something went wrong.

Uh oh!

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

All reactions
return self._ext_to_normal(_getfinalpathname(s))
s = path = _getfullpathname(s)
previous_s = None
if _getfinalpathname is not None:
if strict:
return self._ext_to_normal(_getfinalpathname(s))
tail_parts = [] # End of the path after the first one not found
while True:
try:
s = self._ext_to_normal(_getfinalpathname(s))
except FileNotFoundError:
previous_s = s
s, tail = os.path.split(s)
tail_parts.append(tail)
if previous_s == s:
# Root reached, fallback to _getfullpathname()
return path
else:
tail_parts = [] # End of the path after the first one not found
while True:
try:
s = self._ext_to_normal(_getfinalpathname(s))
except FileNotFoundError:
previous_s = s
s, tail = os.path.split(s)
tail_parts.append(tail)
if previous_s == s:
return path
else:
return os.path.join(s, *reversed(tail_parts))
# Means fallback on absolute
return None
return os.path.join(s, *reversed(tail_parts))

def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix):
prefix = ''
Expand Down
10 changes: 10 additions & 0 deletions 10 Lib/test/test_pathlib.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,16 @@ def test_resolve_dot(self):
# Non-strict
self.assertEqual(r.resolve(strict=False), p / '3' / '4')

def test_resolve_nonexist_relative_issue38671(self):
p = self.cls('non', 'exist')

old_cwd = os.getcwd()
os.chdir(BASE)
try:
self.assertEqual(p.resolve(), self.cls(BASE, p))
finally:
os.chdir(old_cwd)

def test_with(self):
p = self.cls(BASE)
it = p.iterdir()
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2019-12-28-02-50-12.bpo-38671.xT4R0P.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure pathlib.Path.resolve(strict=False) returns an absolute path on
Windows when the path is relative and does not exist.
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.