@@ -208,7 +208,7 @@ def is_legal_py_identifier(s: str) -> bool:
208
208
def ensure_legal_c_identifier (s : str ) -> str :
209
209
# for now, just complain if what we're given isn't legal
210
210
if not is_legal_c_identifier (s ):
211
- fail ("Illegal C identifier: {}" . format ( s ) )
211
+ fail ("Illegal C identifier:" , s )
212
212
# but if we picked a C keyword, pick something else
213
213
if s in c_keywords :
214
214
return s + "_value"
@@ -991,7 +991,7 @@ def parser_body(prototype, *fields, declarations=''):
991
991
argname_fmt = 'PyTuple_GET_ITEM(args, %d)'
992
992
993
993
994
- left_args = "{ } - {}" . format ( nargs , max_pos )
994
+ left_args = f" { nargs } - { max_pos } "
995
995
max_args = NO_VARARG if (vararg != NO_VARARG ) else max_pos
996
996
parser_code = [normalize_snippet ("""
997
997
if (!_PyArg_CheckPositional("{name}", %s, %d, %s)) {{
@@ -1435,7 +1435,7 @@ def render_function(self, clinic, f):
1435
1435
first_optional = min (first_optional , i )
1436
1436
1437
1437
if p .is_vararg ():
1438
- data .cleanup .append ("Py_XDECREF({});" . format ( c .parser_name ) )
1438
+ data .cleanup .append (f "Py_XDECREF({ c .parser_name } );" )
1439
1439
1440
1440
# insert group variable
1441
1441
group = p .group
@@ -1487,8 +1487,7 @@ def render_function(self, clinic, f):
1487
1487
1488
1488
template_dict ['c_basename' ] = c_basename
1489
1489
1490
- methoddef_name = "{}_METHODDEF" .format (c_basename .upper ())
1491
- template_dict ['methoddef_name' ] = methoddef_name
1490
+ template_dict ['methoddef_name' ] = c_basename .upper () + "_METHODDEF"
1492
1491
1493
1492
template_dict ['docstring' ] = self .docstring_for_c_string (f )
1494
1493
@@ -1791,7 +1790,7 @@ def is_stop_line(line):
1791
1790
for field in shlex .split (arguments ):
1792
1791
name , equals , value = field .partition ('=' )
1793
1792
if not equals :
1794
- fail ("Mangled Argument Clinic marker line: {!r}" . format (line ))
1793
+ fail ("Mangled Argument Clinic marker line:" , repr (line ))
1795
1794
d [name .strip ()] = value .strip ()
1796
1795
1797
1796
if self .verify :
@@ -1867,7 +1866,10 @@ def print_block(self, block, *, core_includes=False):
1867
1866
output += '\n '
1868
1867
write (output )
1869
1868
1870
- arguments = "output={} input={}" .format (compute_checksum (output , 16 ), compute_checksum (input , 16 ))
1869
+ arguments = "output={output} input={input}" .format (
1870
+ output = compute_checksum (output , 16 ),
1871
+ input = compute_checksum (input , 16 )
1872
+ )
1871
1873
write (self .language .checksum_line .format (dsl_name = dsl_name , arguments = arguments ))
1872
1874
write ("\n " )
1873
1875
@@ -1976,7 +1978,7 @@ def dump(self):
1976
1978
def file_changed (filename : str , new_contents : str ) -> bool :
1977
1979
"""Return true if file contents changed (meaning we must update it)"""
1978
1980
try :
1979
- with open (filename , 'r' , encoding = "utf-8" ) as fp :
1981
+ with open (filename , encoding = "utf-8" ) as fp :
1980
1982
old_contents = fp .read ()
1981
1983
return old_contents != new_contents
1982
1984
except FileNotFoundError :
@@ -2132,7 +2134,7 @@ def parse(self, input):
2132
2134
dsl_name = block .dsl_name
2133
2135
if dsl_name :
2134
2136
if dsl_name not in self .parsers :
2135
- assert dsl_name in parsers , "No parser to handle {!r} block." . format ( dsl_name )
2137
+ assert dsl_name in parsers , f "No parser to handle { dsl_name !r} block."
2136
2138
self .parsers [dsl_name ] = parsers [dsl_name ](self )
2137
2139
parser = self .parsers [dsl_name ]
2138
2140
try :
@@ -2172,7 +2174,7 @@ def parse(self, input):
2172
2174
"can't make directory {}!" .format (
2173
2175
destination .filename , dirname ))
2174
2176
if self .verify :
2175
- with open (destination .filename , "rt" ) as f :
2177
+ with open (destination .filename ) as f :
2176
2178
parser_2 = BlockParser (f .read (), language = self .language )
2177
2179
blocks = list (parser_2 )
2178
2180
if (len (blocks ) != 1 ) or (blocks [0 ].input != 'preserve\n ' ):
@@ -2239,7 +2241,7 @@ def parse_file(
2239
2241
except KeyError :
2240
2242
fail ("Can't identify file type for file " + repr (filename ))
2241
2243
2242
- with open (filename , 'r' , encoding = "utf-8" ) as f :
2244
+ with open (filename , encoding = "utf-8" ) as f :
2243
2245
raw = f .read ()
2244
2246
2245
2247
# exit quickly if there are no clinic markers in the file
@@ -2537,9 +2539,9 @@ def get_displayname(self, i):
2537
2539
if i == 0 :
2538
2540
return '"argument"'
2539
2541
if not self .is_positional_only ():
2540
- return ''' "argument '{}'"''' . format ( self .name )
2542
+ return f' "argument { self .name !r } "'
2541
2543
else :
2542
- return '"argument {}"' . format ( i )
2544
+ return f '"argument { i } "'
2543
2545
2544
2546
2545
2547
class LandMine :
@@ -2723,7 +2725,8 @@ def __init__(self,
2723
2725
if isinstance (self .default_type , type ):
2724
2726
types_str = self .default_type .__name__
2725
2727
else :
2726
- types_str = ', ' .join ((cls .__name__ for cls in self .default_type ))
2728
+ names = [cls .__name__ for cls in self .default_type ]
2729
+ types_str = ', ' .join (names )
2727
2730
fail ("{}: default value {!r} for field {} is not of type {}" .format (
2728
2731
self .__class__ .__name__ , default , name , types_str ))
2729
2732
self .default = default
@@ -3955,7 +3958,7 @@ def set_template_dict(self, template_dict):
3955
3958
' Py_TYPE({0})->tp_new == base_tp->tp_new)'
3956
3959
).format (self .name )
3957
3960
3958
- line = '{ } &&\n '. format ( type_check )
3961
+ line = f' { type_check } &&\n '
3959
3962
template_dict ['self_type_check' ] = line
3960
3963
3961
3964
type_object = self .function .cls .type_object
@@ -4011,10 +4014,12 @@ def declare(self, data):
4011
4014
data .return_value = data .converter_retval
4012
4015
4013
4016
def err_occurred_if (self , expr , data ):
4014
- data .return_conversion .append ('if (({}) && PyErr_Occurred()) {{\n goto exit;\n }}\n ' .format (expr ))
4017
+ line = f'if (({ expr } ) && PyErr_Occurred()) {{\n goto exit;\n }}\n '
4018
+ data .return_conversion .append (line )
4015
4019
4016
4020
def err_occurred_if_null_pointer (self , variable , data ):
4017
- data .return_conversion .append ('if ({} == NULL) {{\n goto exit;\n }}\n ' .format (variable ))
4021
+ line = f'if ({ variable } == NULL) {{\n goto exit;\n }}\n '
4022
+ data .return_conversion .append (line )
4018
4023
4019
4024
def render (self , function , data ):
4020
4025
"""
@@ -4477,13 +4482,13 @@ def state_modulename_name(self, line):
4477
4482
c_basename = c_basename .strip () or None
4478
4483
4479
4484
if not is_legal_py_identifier (full_name ):
4480
- fail ("Illegal function name: {}" . format ( full_name ) )
4485
+ fail ("Illegal function name:" , full_name )
4481
4486
if c_basename and not is_legal_c_identifier (c_basename ):
4482
- fail ("Illegal C basename: {}" . format ( c_basename ) )
4487
+ fail ("Illegal C basename:" , c_basename )
4483
4488
4484
4489
return_converter = None
4485
4490
if returns :
4486
- ast_input = "def x() -> {}: pass" . format ( returns )
4491
+ ast_input = f "def x() -> { returns } : pass"
4487
4492
module = None
4488
4493
try :
4489
4494
module = ast .parse (ast_input )
@@ -4696,15 +4701,15 @@ def state_parameter(self, line):
4696
4701
4697
4702
module = None
4698
4703
try :
4699
- ast_input = "def x({}): pass" . format ( base )
4704
+ ast_input = f "def x({ base } ): pass"
4700
4705
module = ast .parse (ast_input )
4701
4706
except SyntaxError :
4702
4707
try :
4703
4708
# the last = was probably inside a function call, like
4704
4709
# c: int(accept={str})
4705
4710
# so assume there was no actual default value.
4706
4711
default = None
4707
- ast_input = "def x({}): pass" . format ( line )
4712
+ ast_input = f "def x({ line } ): pass"
4708
4713
module = ast .parse (ast_input )
4709
4714
except SyntaxError :
4710
4715
pass
@@ -4748,8 +4753,7 @@ def state_parameter(self, line):
4748
4753
self .parameter_state = self .ps_optional
4749
4754
default = default .strip ()
4750
4755
bad = False
4751
- ast_input = "x = {}" .format (default )
4752
- bad = False
4756
+ ast_input = f"x = { default } "
4753
4757
try :
4754
4758
module = ast .parse (ast_input )
4755
4759
@@ -4856,7 +4860,7 @@ def bad_node(self, node):
4856
4860
dict = legacy_converters if legacy else converters
4857
4861
legacy_str = "legacy " if legacy else ""
4858
4862
if name not in dict :
4859
- fail ('{ } is not a valid {}converter'. format ( name , legacy_str ) )
4863
+ fail (f' { name } is not a valid { legacy_str } converter' )
4860
4864
# if you use a c_name for the parameter, we just give that name to the converter
4861
4865
# but the parameter object gets the python name
4862
4866
converter = dict [name ](c_name or parameter_name , parameter_name , self .function , value , ** kwargs )
@@ -5388,7 +5392,7 @@ def main(argv):
5388
5392
for parameter_name , parameter in signature .parameters .items ():
5389
5393
if parameter .kind == inspect .Parameter .KEYWORD_ONLY :
5390
5394
if parameter .default != inspect .Parameter .empty :
5391
- s = '{ }={!r}'. format ( parameter_name , parameter . default )
5395
+ s = f' { parameter_name } ={ parameter . default !r} '
5392
5396
else :
5393
5397
s = parameter_name
5394
5398
parameters .append (s )
0 commit comments