Skip to content

Commit

Permalink
Merge branch 'main' into gh-125413-info
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Dec 12, 2024
2 parents 2f4da5d + 292afd1 commit a7cffe7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 73 deletions.
12 changes: 6 additions & 6 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ The following recipes have a more mathematical flavor:
.. testcode::

def powerset(iterable):
"powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
# powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

Expand Down Expand Up @@ -1104,11 +1104,6 @@ The following recipes have a more mathematical flavor:
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
yield from iter_index(data, 1, start=3)

def is_prime(n):
"Return True if n is prime."
# is_prime(1_000_000_000_000_403) → True
return n > 1 and all(n % p for p in sieve(math.isqrt(n) + 1))

def factor(n):
"Prime factors of n."
# factor(99) → 3 3 11
Expand All @@ -1123,6 +1118,11 @@ The following recipes have a more mathematical flavor:
if n > 1:
yield n

def is_prime(n):
"Return True if n is prime."
# is_prime(1_000_000_000_000_403) → True
return n > 1 and next(factor(n)) == n

def totient(n):
"Count of natural numbers up to n that are coprime to n."
# https://mathworld.wolfram.com/TotientFunction.html
Expand Down
54 changes: 0 additions & 54 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,6 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
yield path, dirnames, filenames
paths += [path.joinpath(d) for d in reversed(dirnames)]

def expanduser(self):
""" Return a new path with expanded ~ and ~user constructs
(as returned by os.path.expanduser)
"""
raise UnsupportedOperation(self._unsupported_msg('expanduser()'))

def readlink(self):
"""
Return the path to which the symbolic link points.
Expand All @@ -579,20 +573,6 @@ def _symlink_to_target_of(self, link):
"""
self.symlink_to(link.readlink())

def hardlink_to(self, target):
"""
Make this path a hard link pointing to the same file as *target*.
Note the order of arguments (self, target) is the reverse of os.link's.
"""
raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))

def touch(self, mode=0o666, exist_ok=True):
"""
Create this file with the given access mode, if it doesn't exist.
"""
raise UnsupportedOperation(self._unsupported_msg('touch()'))

def mkdir(self, mode=0o777, parents=False, exist_ok=False):
"""
Create a new directory at this given path.
Expand Down Expand Up @@ -711,37 +691,3 @@ def move_into(self, target_dir):
else:
target = self.with_segments(target_dir, name)
return self.move(target)

def chmod(self, mode, *, follow_symlinks=True):
"""
Change the permissions of the path, like os.chmod().
"""
raise UnsupportedOperation(self._unsupported_msg('chmod()'))

def lchmod(self, mode):
"""
Like chmod(), except if the path points to a symlink, the symlink's
permissions are changed, rather than its target's.
"""
self.chmod(mode, follow_symlinks=False)

def owner(self, *, follow_symlinks=True):
"""
Return the login name of the file owner.
"""
raise UnsupportedOperation(self._unsupported_msg('owner()'))

def group(self, *, follow_symlinks=True):
"""
Return the group name of the file gid.
"""
raise UnsupportedOperation(self._unsupported_msg('group()'))

@classmethod
def from_uri(cls, uri):
"""Return a new path from the given 'file' URI."""
raise UnsupportedOperation(cls._unsupported_msg('from_uri()'))

def as_uri(self):
"""Return the path as a URI."""
raise UnsupportedOperation(self._unsupported_msg('as_uri()'))
28 changes: 27 additions & 1 deletion Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,6 @@ class Path(PathBase, PurePath):
but cannot instantiate a WindowsPath on a POSIX system or vice versa.
"""
__slots__ = ('_status',)
as_uri = PurePath.as_uri

@classmethod
def _unsupported_msg(cls, attribute):
Expand Down Expand Up @@ -922,6 +921,12 @@ def owner(self, *, follow_symlinks=True):
"""
uid = self.stat(follow_symlinks=follow_symlinks).st_uid
return pwd.getpwuid(uid).pw_name
else:
def owner(self, *, follow_symlinks=True):
"""
Return the login name of the file owner.
"""
raise UnsupportedOperation(self._unsupported_msg('owner()'))

if grp:
def group(self, *, follow_symlinks=True):
Expand All @@ -930,6 +935,12 @@ def group(self, *, follow_symlinks=True):
"""
gid = self.stat(follow_symlinks=follow_symlinks).st_gid
return grp.getgrgid(gid).gr_name
else:
def group(self, *, follow_symlinks=True):
"""
Return the group name of the file gid.
"""
raise UnsupportedOperation(self._unsupported_msg('group()'))

if hasattr(os, "readlink"):
def readlink(self):
Expand Down Expand Up @@ -1001,6 +1012,13 @@ def chmod(self, mode, *, follow_symlinks=True):
"""
os.chmod(self, mode, follow_symlinks=follow_symlinks)

def lchmod(self, mode):
"""
Like chmod(), except if the path points to a symlink, the symlink's
permissions are changed, rather than its target's.
"""
self.chmod(mode, follow_symlinks=False)

def unlink(self, missing_ok=False):
"""
Remove this file or link.
Expand Down Expand Up @@ -1097,6 +1115,14 @@ def hardlink_to(self, target):
Note the order of arguments (self, target) is the reverse of os.link's.
"""
os.link(target, self)
else:
def hardlink_to(self, target):
"""
Make this path a hard link pointing to the same file as *target*.
Note the order of arguments (self, target) is the reverse of os.link's.
"""
raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))

def expanduser(self):
""" Return a new path with expanded ~ and ~user constructs
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ def test_fromhex(self):
with self.assertRaises(ValueError) as cm:
self.type2test.fromhex(value)
self.assertIn("fromhex() arg must contain an even number of hexadecimal digits", str(cm.exception))
for value, position in (("a ", 1), (" aa a ", 5), (" aa a a ", 5)):
with self.assertRaises(ValueError) as cm:
self.type2test.fromhex(value)
self.assertIn(f"non-hexadecimal number found in fromhex() arg at position {position}", str(cm.exception))

for data, pos in (
# invalid first hexadecimal character
Expand Down
12 changes: 0 additions & 12 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,21 +1312,9 @@ def test_unsupported_operation(self):
self.assertRaises(e, lambda: list(p.glob('*')))
self.assertRaises(e, lambda: list(p.rglob('*')))
self.assertRaises(e, lambda: list(p.walk()))
self.assertRaises(e, p.expanduser)
self.assertRaises(e, p.readlink)
self.assertRaises(e, p.symlink_to, 'foo')
self.assertRaises(e, p.hardlink_to, 'foo')
self.assertRaises(e, p.mkdir)
self.assertRaises(e, p.touch)
self.assertRaises(e, p.chmod, 0o755)
self.assertRaises(e, p.lchmod, 0o755)
self.assertRaises(e, p.owner)
self.assertRaises(e, p.group)
self.assertRaises(e, p.as_uri)

def test_as_uri_common(self):
e = UnsupportedOperation
self.assertRaises(e, self.cls('').as_uri)

def test_fspath_common(self):
self.assertRaises(TypeError, os.fspath, self.cls(''))
Expand Down

0 comments on commit a7cffe7

Please sign in to comment.