summaryrefslogtreecommitdiffstats
path: root/Tools
diff options
context:
space:
mode:
authorAlex Waygood <Alex.Waygood@Gmail.com>2023-05-20 20:16:49 (GMT)
committerGitHub <noreply@github.com>2023-05-20 20:16:49 (GMT)
commit19dd5aa89af4cc3150ed87f039601f87bc419be7 (patch)
tree0ecc5cf74c28a87cebb02b7370374eaa5d3b138c /Tools
parent30488fa22a8f63753192ae7f1d01665857764e45 (diff)
downloadcpython-19dd5aa89af4cc3150ed87f039601f87bc419be7.zip
cpython-19dd5aa89af4cc3150ed87f039601f87bc419be7.tar.gz
cpython-19dd5aa89af4cc3150ed87f039601f87bc419be7.tar.bz2
gh-104683: Modernise Tools/clinic/ (#104684)
- Make some string interpolations more readable using f-strings or explicit parametrisation - Remove unneeded open() mode specifiers Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Diffstat (limited to 'Tools')
-rwxr-xr-xTools/clinic/clinic.py56
-rw-r--r--Tools/clinic/cpp.py2
2 files changed, 31 insertions, 27 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index b00b480..41e08d1 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -208,7 +208,7 @@ typedef typeof union unsigned void volatile while
def ensure_legal_c_identifier(s: str) -> str:
# for now, just complain if what we're given isn't legal
if not is_legal_c_identifier(s):
- fail("Illegal C identifier: {}".format(s))
+ fail("Illegal C identifier:", s)
# but if we picked a C keyword, pick something else
if s in c_keywords:
return s + "_value"
@@ -991,7 +991,7 @@ class CLanguage(Language):
argname_fmt = 'PyTuple_GET_ITEM(args, %d)'
- left_args = "{} - {}".format(nargs, max_pos)
+ left_args = f"{nargs} - {max_pos}"
max_args = NO_VARARG if (vararg != NO_VARARG) else max_pos
parser_code = [normalize_snippet("""
if (!_PyArg_CheckPositional("{name}", %s, %d, %s)) {{
@@ -1435,7 +1435,7 @@ class CLanguage(Language):
first_optional = min(first_optional, i)
if p.is_vararg():
- data.cleanup.append("Py_XDECREF({});".format(c.parser_name))
+ data.cleanup.append(f"Py_XDECREF({c.parser_name});")
# insert group variable
group = p.group
@@ -1487,8 +1487,7 @@ class CLanguage(Language):
template_dict['c_basename'] = c_basename
- methoddef_name = "{}_METHODDEF".format(c_basename.upper())
- template_dict['methoddef_name'] = methoddef_name
+ template_dict['methoddef_name'] = c_basename.upper() + "_METHODDEF"
template_dict['docstring'] = self.docstring_for_c_string(f)
@@ -1791,7 +1790,7 @@ class BlockParser:
for field in shlex.split(arguments):
name, equals, value = field.partition('=')
if not equals:
- fail("Mangled Argument Clinic marker line: {!r}".format(line))
+ fail("Mangled Argument Clinic marker line:", repr(line))
d[name.strip()] = value.strip()
if self.verify:
@@ -1867,7 +1866,10 @@ class BlockPrinter:
output += '\n'
write(output)
- arguments="output={} input={}".format(compute_checksum(output, 16), compute_checksum(input, 16))
+ arguments = "output={output} input={input}".format(
+ output=compute_checksum(output, 16),
+ input=compute_checksum(input, 16)
+ )
write(self.language.checksum_line.format(dsl_name=dsl_name, arguments=arguments))
write("\n")
@@ -1976,7 +1978,7 @@ return_converters = {}
def file_changed(filename: str, new_contents: str) -> bool:
"""Return true if file contents changed (meaning we must update it)"""
try:
- with open(filename, 'r', encoding="utf-8") as fp:
+ with open(filename, encoding="utf-8") as fp:
old_contents = fp.read()
return old_contents != new_contents
except FileNotFoundError:
@@ -2132,7 +2134,7 @@ impl_definition block
dsl_name = block.dsl_name
if dsl_name:
if dsl_name not in self.parsers:
- assert dsl_name in parsers, "No parser to handle {!r} block.".format(dsl_name)
+ assert dsl_name in parsers, f"No parser to handle {dsl_name!r} block."
self.parsers[dsl_name] = parsers[dsl_name](self)
parser = self.parsers[dsl_name]
try:
@@ -2172,7 +2174,7 @@ impl_definition block
"can't make directory {}!".format(
destination.filename, dirname))
if self.verify:
- with open(destination.filename, "rt") as f:
+ with open(destination.filename) as f:
parser_2 = BlockParser(f.read(), language=self.language)
blocks = list(parser_2)
if (len(blocks) != 1) or (blocks[0].input != 'preserve\n'):
@@ -2239,7 +2241,7 @@ def parse_file(
except KeyError:
fail("Can't identify file type for file " + repr(filename))
- with open(filename, 'r', encoding="utf-8") as f:
+ with open(filename, encoding="utf-8") as f:
raw = f.read()
# exit quickly if there are no clinic markers in the file
@@ -2537,9 +2539,9 @@ class Parameter:
if i == 0:
return '"argument"'
if not self.is_positional_only():
- return '''"argument '{}'"'''.format(self.name)
+ return f'"argument {self.name!r}"'
else:
- return '"argument {}"'.format(i)
+ return f'"argument {i}"'
class LandMine:
@@ -2723,7 +2725,8 @@ class CConverter(metaclass=CConverterAutoRegister):
if isinstance(self.default_type, type):
types_str = self.default_type.__name__
else:
- types_str = ', '.join((cls.__name__ for cls in self.default_type))
+ names = [cls.__name__ for cls in self.default_type]
+ types_str = ', '.join(names)
fail("{}: default value {!r} for field {} is not of type {}".format(
self.__class__.__name__, default, name, types_str))
self.default = default
@@ -3955,7 +3958,7 @@ class self_converter(CConverter):
' Py_TYPE({0})->tp_new == base_tp->tp_new)'
).format(self.name)
- line = '{} &&\n '.format(type_check)
+ line = f'{type_check} &&\n '
template_dict['self_type_check'] = line
type_object = self.function.cls.type_object
@@ -4011,10 +4014,12 @@ class CReturnConverter(metaclass=CReturnConverterAutoRegister):
data.return_value = data.converter_retval
def err_occurred_if(self, expr, data):
- data.return_conversion.append('if (({}) && PyErr_Occurred()) {{\n goto exit;\n}}\n'.format(expr))
+ line = f'if (({expr}) && PyErr_Occurred()) {{\n goto exit;\n}}\n'
+ data.return_conversion.append(line)
def err_occurred_if_null_pointer(self, variable, data):
- data.return_conversion.append('if ({} == NULL) {{\n goto exit;\n}}\n'.format(variable))
+ line = f'if ({variable} == NULL) {{\n goto exit;\n}}\n'
+ data.return_conversion.append(line)
def render(self, function, data):
"""
@@ -4477,13 +4482,13 @@ class DSLParser:
c_basename = c_basename.strip() or None
if not is_legal_py_identifier(full_name):
- fail("Illegal function name: {}".format(full_name))
+ fail("Illegal function name:", full_name)
if c_basename and not is_legal_c_identifier(c_basename):
- fail("Illegal C basename: {}".format(c_basename))
+ fail("Illegal C basename:", c_basename)
return_converter = None
if returns:
- ast_input = "def x() -> {}: pass".format(returns)
+ ast_input = f"def x() -> {returns}: pass"
module = None
try:
module = ast.parse(ast_input)
@@ -4696,7 +4701,7 @@ class DSLParser:
module = None
try:
- ast_input = "def x({}): pass".format(base)
+ ast_input = f"def x({base}): pass"
module = ast.parse(ast_input)
except SyntaxError:
try:
@@ -4704,7 +4709,7 @@ class DSLParser:
# c: int(accept={str})
# so assume there was no actual default value.
default = None
- ast_input = "def x({}): pass".format(line)
+ ast_input = f"def x({line}): pass"
module = ast.parse(ast_input)
except SyntaxError:
pass
@@ -4748,8 +4753,7 @@ class DSLParser:
self.parameter_state = self.ps_optional
default = default.strip()
bad = False
- ast_input = "x = {}".format(default)
- bad = False
+ ast_input = f"x = {default}"
try:
module = ast.parse(ast_input)
@@ -4856,7 +4860,7 @@ class DSLParser:
dict = legacy_converters if legacy else converters
legacy_str = "legacy " if legacy else ""
if name not in dict:
- fail('{} is not a valid {}converter'.format(name, legacy_str))
+ fail(f'{name} is not a valid {legacy_str}converter')
# if you use a c_name for the parameter, we just give that name to the converter
# but the parameter object gets the python name
converter = dict[name](c_name or parameter_name, parameter_name, self.function, value, **kwargs)
@@ -5388,7 +5392,7 @@ For more information see https://docs.python.org/3/howto/clinic.html""")
for parameter_name, parameter in signature.parameters.items():
if parameter.kind == inspect.Parameter.KEYWORD_ONLY:
if parameter.default != inspect.Parameter.empty:
- s = '{}={!r}'.format(parameter_name, parameter.default)
+ s = f'{parameter_name}={parameter.default!r}'
else:
s = parameter_name
parameters.append(s)
diff --git a/Tools/clinic/cpp.py b/Tools/clinic/cpp.py
index a3546f5..c1a2eee 100644
--- a/Tools/clinic/cpp.py
+++ b/Tools/clinic/cpp.py
@@ -185,7 +185,7 @@ class Monitor:
if __name__ == '__main__':
for filename in sys.argv[1:]:
- with open(filename, "rt") as f:
+ with open(filename) as f:
cpp = Monitor(filename, verbose=True)
print()
print(filename)