Skip to content

bpo-39924: handle missing os functions more consistently in pathlib #19220

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

Merged
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
43 changes: 16 additions & 27 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@
from urllib.parse import quote_from_bytes as urlquote_from_bytes


supports_symlinks = True
if os.name == 'nt':
import nt
if sys.getwindowsversion()[:2] >= (6, 0):
from nt import _getfinalpathname
else:
supports_symlinks = False
_getfinalpathname = None
from nt import _getfinalpathname
else:
nt = None
_getfinalpathname = None


__all__ = [
Expand Down Expand Up @@ -412,18 +406,17 @@ class _NormalAccessor(_Accessor):
if hasattr(os, "lchmod"):
lchmod = os.lchmod
else:
def lchmod(self, pathobj, mode):
raise NotImplementedError("lchmod() not available on this system")
def lchmod(self, path, mode):
raise NotImplementedError("os.lchmod() not available on this system")

mkdir = os.mkdir

unlink = os.unlink

if hasattr(os, "link"):
link_to = os.link
link = os.link
else:
@staticmethod
def link_to(self, target):
def link(self, src, dst):
raise NotImplementedError("os.link() not available on this system")

rmdir = os.rmdir
Expand All @@ -432,23 +425,19 @@ def link_to(self, target):

replace = os.replace

if nt:
if supports_symlinks:
symlink = os.symlink
else:
def symlink(a, b, target_is_directory):
raise NotImplementedError("symlink() not available on this system")
if hasattr(os, "symlink"):
symlink = os.symlink
else:
# Under POSIX, os.symlink() takes two args
@staticmethod
def symlink(a, b, target_is_directory):
return os.symlink(a, b)
def symlink(self, src, dst, target_is_directory=False):
raise NotImplementedError("os.symlink() not available on this system")

utime = os.utime

# Helper for resolve()
def readlink(self, path):
return os.readlink(path)
if hasattr(os, "readlink"):
readlink = os.readlink
else:
def readlink(self, path):
raise NotImplementedError("os.readlink() not available on this system")

def owner(self, path):
try:
Expand Down Expand Up @@ -1365,7 +1354,7 @@ def link_to(self, target):
"""
Create a hard link pointing to a path named target.
"""
self._accessor.link_to(self, target)
self._accessor.link(self, target)

def rename(self, target):
"""
Expand Down