Skip to content

Commit a7cffe7

Browse files
committed
Merge branch 'main' into gh-125413-info
2 parents 2f4da5d + 292afd1 commit a7cffe7

File tree

5 files changed

+37
-73
lines changed

5 files changed

+37
-73
lines changed

Doc/library/itertools.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ The following recipes have a more mathematical flavor:
10151015
.. testcode::
10161016

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

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

1107-
def is_prime(n):
1108-
"Return True if n is prime."
1109-
# is_prime(1_000_000_000_000_403) → True
1110-
return n > 1 and all(n % p for p in sieve(math.isqrt(n) + 1))
1111-
11121107
def factor(n):
11131108
"Prime factors of n."
11141109
# factor(99) → 3 3 11
@@ -1123,6 +1118,11 @@ The following recipes have a more mathematical flavor:
11231118
if n > 1:
11241119
yield n
11251120

1121+
def is_prime(n):
1122+
"Return True if n is prime."
1123+
# is_prime(1_000_000_000_000_403) → True
1124+
return n > 1 and next(factor(n)) == n
1125+
11261126
def totient(n):
11271127
"Count of natural numbers up to n that are coprime to n."
11281128
# https://mathworld.wolfram.com/TotientFunction.html

Lib/pathlib/_abc.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,6 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
553553
yield path, dirnames, filenames
554554
paths += [path.joinpath(d) for d in reversed(dirnames)]
555555

556-
def expanduser(self):
557-
""" Return a new path with expanded ~ and ~user constructs
558-
(as returned by os.path.expanduser)
559-
"""
560-
raise UnsupportedOperation(self._unsupported_msg('expanduser()'))
561-
562556
def readlink(self):
563557
"""
564558
Return the path to which the symbolic link points.
@@ -579,20 +573,6 @@ def _symlink_to_target_of(self, link):
579573
"""
580574
self.symlink_to(link.readlink())
581575

582-
def hardlink_to(self, target):
583-
"""
584-
Make this path a hard link pointing to the same file as *target*.
585-
586-
Note the order of arguments (self, target) is the reverse of os.link's.
587-
"""
588-
raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))
589-
590-
def touch(self, mode=0o666, exist_ok=True):
591-
"""
592-
Create this file with the given access mode, if it doesn't exist.
593-
"""
594-
raise UnsupportedOperation(self._unsupported_msg('touch()'))
595-
596576
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
597577
"""
598578
Create a new directory at this given path.
@@ -711,37 +691,3 @@ def move_into(self, target_dir):
711691
else:
712692
target = self.with_segments(target_dir, name)
713693
return self.move(target)
714-
715-
def chmod(self, mode, *, follow_symlinks=True):
716-
"""
717-
Change the permissions of the path, like os.chmod().
718-
"""
719-
raise UnsupportedOperation(self._unsupported_msg('chmod()'))
720-
721-
def lchmod(self, mode):
722-
"""
723-
Like chmod(), except if the path points to a symlink, the symlink's
724-
permissions are changed, rather than its target's.
725-
"""
726-
self.chmod(mode, follow_symlinks=False)
727-
728-
def owner(self, *, follow_symlinks=True):
729-
"""
730-
Return the login name of the file owner.
731-
"""
732-
raise UnsupportedOperation(self._unsupported_msg('owner()'))
733-
734-
def group(self, *, follow_symlinks=True):
735-
"""
736-
Return the group name of the file gid.
737-
"""
738-
raise UnsupportedOperation(self._unsupported_msg('group()'))
739-
740-
@classmethod
741-
def from_uri(cls, uri):
742-
"""Return a new path from the given 'file' URI."""
743-
raise UnsupportedOperation(cls._unsupported_msg('from_uri()'))
744-
745-
def as_uri(self):
746-
"""Return the path as a URI."""
747-
raise UnsupportedOperation(self._unsupported_msg('as_uri()'))

Lib/pathlib/_local.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ class Path(PathBase, PurePath):
624624
but cannot instantiate a WindowsPath on a POSIX system or vice versa.
625625
"""
626626
__slots__ = ('_status',)
627-
as_uri = PurePath.as_uri
628627

629628
@classmethod
630629
def _unsupported_msg(cls, attribute):
@@ -922,6 +921,12 @@ def owner(self, *, follow_symlinks=True):
922921
"""
923922
uid = self.stat(follow_symlinks=follow_symlinks).st_uid
924923
return pwd.getpwuid(uid).pw_name
924+
else:
925+
def owner(self, *, follow_symlinks=True):
926+
"""
927+
Return the login name of the file owner.
928+
"""
929+
raise UnsupportedOperation(self._unsupported_msg('owner()'))
925930

926931
if grp:
927932
def group(self, *, follow_symlinks=True):
@@ -930,6 +935,12 @@ def group(self, *, follow_symlinks=True):
930935
"""
931936
gid = self.stat(follow_symlinks=follow_symlinks).st_gid
932937
return grp.getgrgid(gid).gr_name
938+
else:
939+
def group(self, *, follow_symlinks=True):
940+
"""
941+
Return the group name of the file gid.
942+
"""
943+
raise UnsupportedOperation(self._unsupported_msg('group()'))
933944

934945
if hasattr(os, "readlink"):
935946
def readlink(self):
@@ -1001,6 +1012,13 @@ def chmod(self, mode, *, follow_symlinks=True):
10011012
"""
10021013
os.chmod(self, mode, follow_symlinks=follow_symlinks)
10031014

1015+
def lchmod(self, mode):
1016+
"""
1017+
Like chmod(), except if the path points to a symlink, the symlink's
1018+
permissions are changed, rather than its target's.
1019+
"""
1020+
self.chmod(mode, follow_symlinks=False)
1021+
10041022
def unlink(self, missing_ok=False):
10051023
"""
10061024
Remove this file or link.
@@ -1097,6 +1115,14 @@ def hardlink_to(self, target):
10971115
Note the order of arguments (self, target) is the reverse of os.link's.
10981116
"""
10991117
os.link(target, self)
1118+
else:
1119+
def hardlink_to(self, target):
1120+
"""
1121+
Make this path a hard link pointing to the same file as *target*.
1122+
1123+
Note the order of arguments (self, target) is the reverse of os.link's.
1124+
"""
1125+
raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))
11001126

11011127
def expanduser(self):
11021128
""" Return a new path with expanded ~ and ~user constructs

Lib/test/test_bytes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,10 @@ def test_fromhex(self):
464464
with self.assertRaises(ValueError) as cm:
465465
self.type2test.fromhex(value)
466466
self.assertIn("fromhex() arg must contain an even number of hexadecimal digits", str(cm.exception))
467+
for value, position in (("a ", 1), (" aa a ", 5), (" aa a a ", 5)):
468+
with self.assertRaises(ValueError) as cm:
469+
self.type2test.fromhex(value)
470+
self.assertIn(f"non-hexadecimal number found in fromhex() arg at position {position}", str(cm.exception))
467471

468472
for data, pos in (
469473
# invalid first hexadecimal character

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,21 +1312,9 @@ def test_unsupported_operation(self):
13121312
self.assertRaises(e, lambda: list(p.glob('*')))
13131313
self.assertRaises(e, lambda: list(p.rglob('*')))
13141314
self.assertRaises(e, lambda: list(p.walk()))
1315-
self.assertRaises(e, p.expanduser)
13161315
self.assertRaises(e, p.readlink)
13171316
self.assertRaises(e, p.symlink_to, 'foo')
1318-
self.assertRaises(e, p.hardlink_to, 'foo')
13191317
self.assertRaises(e, p.mkdir)
1320-
self.assertRaises(e, p.touch)
1321-
self.assertRaises(e, p.chmod, 0o755)
1322-
self.assertRaises(e, p.lchmod, 0o755)
1323-
self.assertRaises(e, p.owner)
1324-
self.assertRaises(e, p.group)
1325-
self.assertRaises(e, p.as_uri)
1326-
1327-
def test_as_uri_common(self):
1328-
e = UnsupportedOperation
1329-
self.assertRaises(e, self.cls('').as_uri)
13301318

13311319
def test_fspath_common(self):
13321320
self.assertRaises(TypeError, os.fspath, self.cls(''))

0 commit comments

Comments
 (0)