Skip to content

Commit 863a5b5

Browse files
authored
Merge pull request #5134 from NakanoMiku39/main
Update libraries and test files from CPython v3.12
2 parents fb12a4c + 1c6336b commit 863a5b5

File tree

14 files changed

+2259
-83
lines changed

14 files changed

+2259
-83
lines changed

Lib/abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class that has a metaclass derived from ABCMeta cannot be
1818
1919
class C(metaclass=ABCMeta):
2020
@abstractmethod
21-
def my_abstract_method(self, ...):
21+
def my_abstract_method(self, arg1, arg2, argN):
2222
...
2323
"""
2424
funcobj.__isabstractmethod__ = True

Lib/argparse.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -345,21 +345,22 @@ def _format_usage(self, usage, actions, groups, prefix):
345345
def get_lines(parts, indent, prefix=None):
346346
lines = []
347347
line = []
348+
indent_length = len(indent)
348349
if prefix is not None:
349350
line_len = len(prefix) - 1
350351
else:
351-
line_len = len(indent) - 1
352+
line_len = indent_length - 1
352353
for part in parts:
353354
if line_len + 1 + len(part) > text_width and line:
354355
lines.append(indent + ' '.join(line))
355356
line = []
356-
line_len = len(indent) - 1
357+
line_len = indent_length - 1
357358
line.append(part)
358359
line_len += len(part) + 1
359360
if line:
360361
lines.append(indent + ' '.join(line))
361362
if prefix is not None:
362-
lines[0] = lines[0][len(indent):]
363+
lines[0] = lines[0][indent_length:]
363364
return lines
364365

365366
# if prog is short, follow it with optionals or positionals
@@ -403,10 +404,18 @@ def _format_actions_usage(self, actions, groups):
403404
except ValueError:
404405
continue
405406
else:
406-
end = start + len(group._group_actions)
407+
group_action_count = len(group._group_actions)
408+
end = start + group_action_count
407409
if actions[start:end] == group._group_actions:
410+
411+
suppressed_actions_count = 0
408412
for action in group._group_actions:
409413
group_actions.add(action)
414+
if action.help is SUPPRESS:
415+
suppressed_actions_count += 1
416+
417+
exposed_actions_count = group_action_count - suppressed_actions_count
418+
410419
if not group.required:
411420
if start in inserts:
412421
inserts[start] += ' ['
@@ -416,7 +425,7 @@ def _format_actions_usage(self, actions, groups):
416425
inserts[end] += ']'
417426
else:
418427
inserts[end] = ']'
419-
else:
428+
elif exposed_actions_count > 1:
420429
if start in inserts:
421430
inserts[start] += ' ('
422431
else:
@@ -490,7 +499,6 @@ def _format_actions_usage(self, actions, groups):
490499
text = _re.sub(r'(%s) ' % open, r'\1', text)
491500
text = _re.sub(r' (%s)' % close, r'\1', text)
492501
text = _re.sub(r'%s *%s' % (open, close), r'', text)
493-
text = _re.sub(r'\(([^|]*)\)', r'\1', text)
494502
text = text.strip()
495503

496504
# return the text
@@ -875,16 +883,19 @@ def __call__(self, parser, namespace, values, option_string=None):
875883
raise NotImplementedError(_('.__call__() not defined'))
876884

877885

886+
# FIXME: remove together with `BooleanOptionalAction` deprecated arguments.
887+
_deprecated_default = object()
888+
878889
class BooleanOptionalAction(Action):
879890
def __init__(self,
880891
option_strings,
881892
dest,
882893
default=None,
883-
type=None,
884-
choices=None,
894+
type=_deprecated_default,
895+
choices=_deprecated_default,
885896
required=False,
886897
help=None,
887-
metavar=None):
898+
metavar=_deprecated_default):
888899

889900
_option_strings = []
890901
for option_string in option_strings:
@@ -894,6 +905,24 @@ def __init__(self,
894905
option_string = '--no-' + option_string[2:]
895906
_option_strings.append(option_string)
896907

908+
# We need `_deprecated` special value to ban explicit arguments that
909+
# match default value. Like:
910+
# parser.add_argument('-f', action=BooleanOptionalAction, type=int)
911+
for field_name in ('type', 'choices', 'metavar'):
912+
if locals()[field_name] is not _deprecated_default:
913+
warnings._deprecated(
914+
field_name,
915+
"{name!r} is deprecated as of Python 3.12 and will be "
916+
"removed in Python {remove}.",
917+
remove=(3, 14))
918+
919+
if type is _deprecated_default:
920+
type = None
921+
if choices is _deprecated_default:
922+
choices = None
923+
if metavar is _deprecated_default:
924+
metavar = None
925+
897926
super().__init__(
898927
option_strings=_option_strings,
899928
dest=dest,
@@ -2165,7 +2194,9 @@ def _read_args_from_files(self, arg_strings):
21652194
# replace arguments referencing files with the file content
21662195
else:
21672196
try:
2168-
with open(arg_string[1:]) as args_file:
2197+
with open(arg_string[1:],
2198+
encoding=_sys.getfilesystemencoding(),
2199+
errors=_sys.getfilesystemencodeerrors()) as args_file:
21692200
arg_strings = []
21702201
for arg_line in args_file.read().splitlines():
21712202
for arg in self.convert_arg_line_to_args(arg_line):
@@ -2479,9 +2510,11 @@ def _get_values(self, action, arg_strings):
24792510
not action.option_strings):
24802511
if action.default is not None:
24812512
value = action.default
2513+
self._check_value(action, value)
24822514
else:
2515+
# since arg_strings is always [] at this point
2516+
# there is no need to use self._check_value(action, value)
24832517
value = arg_strings
2484-
self._check_value(action, value)
24852518

24862519
# single argument or optional argument produces a single value
24872520
elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
@@ -2523,7 +2556,6 @@ def _get_value(self, action, arg_string):
25232556

25242557
# ArgumentTypeErrors indicate errors
25252558
except ArgumentTypeError as err:
2526-
name = getattr(action.type, '__name__', repr(action.type))
25272559
msg = str(err)
25282560
raise ArgumentError(action, msg)
25292561

@@ -2595,9 +2627,11 @@ def print_help(self, file=None):
25952627

25962628
def _print_message(self, message, file=None):
25972629
if message:
2598-
if file is None:
2599-
file = _sys.stderr
2600-
file.write(message)
2630+
file = file or _sys.stderr
2631+
try:
2632+
file.write(message)
2633+
except (AttributeError, OSError):
2634+
pass
26012635

26022636
# ===============
26032637
# Exiting methods

Lib/base64.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -508,25 +508,16 @@ def b85decode(b):
508508

509509
def encode(input, output):
510510
"""Encode a file; input and output are binary files."""
511-
while True:
512-
s = input.read(MAXBINSIZE)
513-
if not s:
514-
break
515-
while len(s) < MAXBINSIZE:
516-
ns = input.read(MAXBINSIZE-len(s))
517-
if not ns:
518-
break
511+
while s := input.read(MAXBINSIZE):
512+
while len(s) < MAXBINSIZE and (ns := input.read(MAXBINSIZE-len(s))):
519513
s += ns
520514
line = binascii.b2a_base64(s)
521515
output.write(line)
522516

523517

524518
def decode(input, output):
525519
"""Decode a file; input and output are binary files."""
526-
while True:
527-
line = input.readline()
528-
if not line:
529-
break
520+
while line := input.readline():
530521
s = binascii.a2b_base64(line)
531522
output.write(s)
532523

@@ -567,13 +558,12 @@ def decodebytes(s):
567558
def main():
568559
"""Small main program"""
569560
import sys, getopt
570-
usage = """usage: %s [-h|-d|-e|-u|-t] [file|-]
561+
usage = f"""usage: {sys.argv[0]} [-h|-d|-e|-u] [file|-]
571562
-h: print this help message and exit
572563
-d, -u: decode
573-
-e: encode (default)
574-
-t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
564+
-e: encode (default)"""
575565
try:
576-
opts, args = getopt.getopt(sys.argv[1:], 'hdeut')
566+
opts, args = getopt.getopt(sys.argv[1:], 'hdeu')
577567
except getopt.error as msg:
578568
sys.stdout = sys.stderr
579569
print(msg)
@@ -584,7 +574,6 @@ def main():
584574
if o == '-e': func = encode
585575
if o == '-d': func = decode
586576
if o == '-u': func = decode
587-
if o == '-t': test(); return
588577
if o == '-h': print(usage); return
589578
if args and args[0] != '-':
590579
with open(args[0], 'rb') as f:
@@ -593,15 +582,5 @@ def main():
593582
func(sys.stdin.buffer, sys.stdout.buffer)
594583

595584

596-
def test():
597-
s0 = b"Aladdin:open sesame"
598-
print(repr(s0))
599-
s1 = encodebytes(s0)
600-
print(repr(s1))
601-
s2 = decodebytes(s1)
602-
print(repr(s2))
603-
assert s0 == s2
604-
605-
606585
if __name__ == '__main__':
607586
main()

Lib/bdb.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,12 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
570570
rv = frame.f_locals['__return__']
571571
s += '->'
572572
s += reprlib.repr(rv)
573-
line = linecache.getline(filename, lineno, frame.f_globals)
574-
if line:
575-
s += lprefix + line.strip()
573+
if lineno is not None:
574+
line = linecache.getline(filename, lineno, frame.f_globals)
575+
if line:
576+
s += lprefix + line.strip()
577+
else:
578+
s += f'{lprefix}Warning: lineno is None'
576579
return s
577580

578581
# The following methods can be called by clients to use
@@ -805,15 +808,18 @@ def checkfuncname(b, frame):
805808
return True
806809

807810

808-
# Determines if there is an effective (active) breakpoint at this
809-
# line of code. Returns breakpoint number or 0 if none
810811
def effective(file, line, frame):
811-
"""Determine which breakpoint for this file:line is to be acted upon.
812+
"""Return (active breakpoint, delete temporary flag) or (None, None) as
813+
breakpoint to act upon.
814+
815+
The "active breakpoint" is the first entry in bplist[line, file] (which
816+
must exist) that is enabled, for which checkfuncname is True, and that
817+
has neither a False condition nor a positive ignore count. The flag,
818+
meaning that a temporary breakpoint should be deleted, is False only
819+
when the condiion cannot be evaluated (in which case, ignore count is
820+
ignored).
812821
813-
Called only if we know there is a breakpoint at this location. Return
814-
the breakpoint that was triggered and a boolean that indicates if it is
815-
ok to delete a temporary breakpoint. Return (None, None) if there is no
816-
matching breakpoint.
822+
If no such entry exists, then (None, None) is returned.
817823
"""
818824
possibles = Breakpoint.bplist[file, line]
819825
for b in possibles:

Lib/bisect.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def insort_right(a, x, lo=0, hi=None, *, key=None):
88
99
Optional args lo (default 0) and hi (default len(a)) bound the
1010
slice of a to be searched.
11+
12+
A custom key function can be supplied to customize the sort order.
1113
"""
1214
if key is None:
1315
lo = bisect_right(a, x, lo, hi)
@@ -25,6 +27,8 @@ def bisect_right(a, x, lo=0, hi=None, *, key=None):
2527
2628
Optional args lo (default 0) and hi (default len(a)) bound the
2729
slice of a to be searched.
30+
31+
A custom key function can be supplied to customize the sort order.
2832
"""
2933

3034
if lo < 0:
@@ -57,6 +61,8 @@ def insort_left(a, x, lo=0, hi=None, *, key=None):
5761
5862
Optional args lo (default 0) and hi (default len(a)) bound the
5963
slice of a to be searched.
64+
65+
A custom key function can be supplied to customize the sort order.
6066
"""
6167

6268
if key is None:
@@ -74,6 +80,8 @@ def bisect_left(a, x, lo=0, hi=None, *, key=None):
7480
7581
Optional args lo (default 0) and hi (default len(a)) bound the
7682
slice of a to be searched.
83+
84+
A custom key function can be supplied to customize the sort order.
7785
"""
7886

7987
if lo < 0:

0 commit comments

Comments
 (0)