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-39659: Route calls from pathlib.Path to os.getcwd() via the path accessor #18834

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def compile_pattern(self, pattern):
def resolve(self, path, strict=False):
s = str(path)
if not s:
return os.getcwd()
return path._accessor.getcwd()
previous_s = None
if _getfinalpathname is not None:
if strict:
Expand Down Expand Up @@ -352,7 +352,7 @@ def _resolve(path, rest):
return path
# NOTE: according to POSIX, getcwd() cannot contain path components
# which are symlinks.
base = '' if path.is_absolute() else os.getcwd()
base = '' if path.is_absolute() else accessor.getcwd()
return _resolve(base, str(path)) or sep

def is_reserved(self, parts):
Expand Down Expand Up @@ -469,6 +469,8 @@ def group(self, path):
except ImportError:
raise NotImplementedError("Path.group() is unsupported on this system")

getcwd = os.getcwd


_normal_accessor = _NormalAccessor()

Expand Down Expand Up @@ -1104,7 +1106,7 @@ def cwd(cls):
"""Return a new path pointing to the current working directory
(as returned by os.getcwd()).
"""
return cls(os.getcwd())
return cls(cls()._accessor.getcwd())
Copy link
Member

Choose a reason for hiding this comment

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

This is a quirky way to implement it. If the accessor becomes tied to the class rather than specific instances, perhaps it should become a class-level attribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's very similar to how home() is implemented immediately below.

Agree generally that the accessor should be a class attribute and not an instance attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've replaced cls() with cls in this method, and home() for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, that didn't work at all! Looks like:

Reverting to previous patch (the one you reviewed).


@classmethod
def home(cls):
Expand Down Expand Up @@ -1173,7 +1175,7 @@ def absolute(self):
return self
# FIXME this must defer to the specific flavour (and, under Windows,
# use nt._getfullpathname())
return self._from_parts([os.getcwd()] + self._parts)
return self._from_parts([self._accessor.getcwd()] + self._parts)

def resolve(self, strict=False):
"""
Expand Down