Skip to content

Commit a4bdc72

Browse files
committed
Merge pull request pypa/distutils#256 from DimitriPapadopoulos/UP
Apply ruff/pyupgrade rules (UP)
2 parents cb7f154 + 1072cbd commit a4bdc72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+202
-212
lines changed

distutils/_modified.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def newer(source, target):
2424
Raises DistutilsFileError if 'source' does not exist.
2525
"""
2626
if not os.path.exists(source):
27-
raise DistutilsFileError("file '%s' does not exist" % os.path.abspath(source))
27+
raise DistutilsFileError(f"file '{os.path.abspath(source)}' does not exist")
2828

2929
return _newer(source, target)
3030

distutils/archive_util.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _set_uid_gid(tarinfo):
113113
return tarinfo
114114

115115
if not dry_run:
116-
tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
116+
tar = tarfile.open(archive_name, f'w|{tar_compression[compress]}')
117117
try:
118118
tar.add(base_dir, filter=_set_uid_gid)
119119
finally:
@@ -160,12 +160,9 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0): # noqa: C901
160160
# XXX really should distinguish between "couldn't find
161161
# external 'zip' command" and "zip failed".
162162
raise DistutilsExecError(
163-
(
164-
"unable to create zip file '%s': "
165-
"could neither import the 'zipfile' module nor "
166-
"find a standalone zip utility"
167-
)
168-
% zip_filename
163+
f"unable to create zip file '{zip_filename}': "
164+
"could neither import the 'zipfile' module nor "
165+
"find a standalone zip utility"
169166
)
170167

171168
else:
@@ -260,7 +257,7 @@ def make_archive(
260257
try:
261258
format_info = ARCHIVE_FORMATS[format]
262259
except KeyError:
263-
raise ValueError("unknown archive format '%s'" % format)
260+
raise ValueError(f"unknown archive format '{format}'")
264261

265262
func = format_info[0]
266263
for arg, val in format_info[1]:

distutils/bcppcompiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ def link( # noqa: C901
234234
head, tail = os.path.split(output_filename)
235235
modname, ext = os.path.splitext(tail)
236236
temp_dir = os.path.dirname(objects[0]) # preserve tree structure
237-
def_file = os.path.join(temp_dir, '%s.def' % modname)
237+
def_file = os.path.join(temp_dir, f'{modname}.def')
238238
contents = ['EXPORTS']
239239
for sym in export_symbols or []:
240240
contents.append(f' {sym}=_{sym}')
241-
self.execute(write_file, (def_file, contents), "writing %s" % def_file)
241+
self.execute(write_file, (def_file, contents), f"writing {def_file}")
242242

243243
# Borland C++ has problems with '/' in paths
244244
objects2 = map(os.path.normpath, objects)
@@ -254,7 +254,7 @@ def link( # noqa: C901
254254
objects.append(file)
255255

256256
for ell in library_dirs:
257-
ld_args.append("/L%s" % os.path.normpath(ell))
257+
ld_args.append(f"/L{os.path.normpath(ell)}")
258258
ld_args.append("/L.") # we sometimes use relative paths
259259

260260
# list of object files

distutils/ccompiler.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def _check_macro_definitions(self, definitions):
202202
and isinstance(defn[0], str)
203203
):
204204
raise TypeError(
205-
("invalid macro definition '%s': " % defn)
205+
(f"invalid macro definition '{defn}': ")
206206
+ "must be tuple (string,), (string, string), or "
207207
+ "(string, None)"
208208
)
@@ -859,7 +859,7 @@ def has_function( # noqa: C901
859859
fd, fname = tempfile.mkstemp(".c", funcname, text=True)
860860
with os.fdopen(fd, "w", encoding='utf-8') as f:
861861
for incl in includes:
862-
f.write("""#include "%s"\n""" % incl)
862+
f.write(f"""#include "{incl}"\n""")
863863
if not includes:
864864
# Use "char func(void);" as the prototype to follow
865865
# what autoconf does. This prototype does not match
@@ -869,22 +869,20 @@ def has_function( # noqa: C901
869869
# know the exact argument types, and the has_function
870870
# interface does not provide that level of information.
871871
f.write(
872-
"""\
872+
f"""\
873873
#ifdef __cplusplus
874874
extern "C"
875875
#endif
876-
char %s(void);
876+
char {funcname}(void);
877877
"""
878-
% funcname
879878
)
880879
f.write(
881-
"""\
882-
int main (int argc, char **argv) {
883-
%s();
880+
f"""\
881+
int main (int argc, char **argv) {{
882+
{funcname}();
884883
return 0;
885-
}
884+
}}
886885
"""
887-
% funcname
888886
)
889887

890888
try:
@@ -1032,7 +1030,7 @@ def debug_print(self, msg):
10321030
print(msg)
10331031

10341032
def warn(self, msg):
1035-
sys.stderr.write("warning: %s\n" % msg)
1033+
sys.stderr.write(f"warning: {msg}\n")
10361034

10371035
def execute(self, func, args, msg=None, level=1):
10381036
execute(func, args, msg, self.dry_run)
@@ -1145,9 +1143,9 @@ def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
11451143

11461144
(module_name, class_name, long_description) = compiler_class[compiler]
11471145
except KeyError:
1148-
msg = "don't know how to compile C/C++ code on platform '%s'" % plat
1146+
msg = f"don't know how to compile C/C++ code on platform '{plat}'"
11491147
if compiler is not None:
1150-
msg = msg + " with '%s' compiler" % compiler
1148+
msg = msg + f" with '{compiler}' compiler"
11511149
raise DistutilsPlatformError(msg)
11521150

11531151
try:
@@ -1157,7 +1155,7 @@ def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
11571155
klass = vars(module)[class_name]
11581156
except ImportError:
11591157
raise DistutilsModuleError(
1160-
"can't compile C/C++ code: unable to load module '%s'" % module_name
1158+
f"can't compile C/C++ code: unable to load module '{module_name}'"
11611159
)
11621160
except KeyError:
11631161
raise DistutilsModuleError(
@@ -1196,23 +1194,23 @@ def gen_preprocess_options(macros, include_dirs):
11961194
for macro in macros:
11971195
if not (isinstance(macro, tuple) and 1 <= len(macro) <= 2):
11981196
raise TypeError(
1199-
"bad macro definition '%s': "
1200-
"each element of 'macros' list must be a 1- or 2-tuple" % macro
1197+
f"bad macro definition '{macro}': "
1198+
"each element of 'macros' list must be a 1- or 2-tuple"
12011199
)
12021200

12031201
if len(macro) == 1: # undefine this macro
1204-
pp_opts.append("-U%s" % macro[0])
1202+
pp_opts.append(f"-U{macro[0]}")
12051203
elif len(macro) == 2:
12061204
if macro[1] is None: # define with no explicit value
1207-
pp_opts.append("-D%s" % macro[0])
1205+
pp_opts.append(f"-D{macro[0]}")
12081206
else:
12091207
# XXX *don't* need to be clever about quoting the
12101208
# macro value here, because we're going to avoid the
12111209
# shell at all costs when we spawn the command!
12121210
pp_opts.append("-D{}={}".format(*macro))
12131211

12141212
for dir in include_dirs:
1215-
pp_opts.append("-I%s" % dir)
1213+
pp_opts.append(f"-I{dir}")
12161214
return pp_opts
12171215

12181216

@@ -1245,7 +1243,7 @@ def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
12451243
lib_opts.append(lib_file)
12461244
else:
12471245
compiler.warn(
1248-
"no library file corresponding to '%s' found (skipping)" % lib
1246+
f"no library file corresponding to '{lib}' found (skipping)"
12491247
)
12501248
else:
12511249
lib_opts.append(compiler.library_option(lib))

distutils/cmd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def initialize_options(self):
135135
This method must be implemented by all command classes.
136136
"""
137137
raise RuntimeError(
138-
"abstract method -- subclass %s must override" % self.__class__
138+
f"abstract method -- subclass {self.__class__} must override"
139139
)
140140

141141
def finalize_options(self):
@@ -150,14 +150,14 @@ def finalize_options(self):
150150
This method must be implemented by all command classes.
151151
"""
152152
raise RuntimeError(
153-
"abstract method -- subclass %s must override" % self.__class__
153+
f"abstract method -- subclass {self.__class__} must override"
154154
)
155155

156156
def dump_options(self, header=None, indent=""):
157157
from distutils.fancy_getopt import longopt_xlate
158158

159159
if header is None:
160-
header = "command options for '%s':" % self.get_command_name()
160+
header = f"command options for '{self.get_command_name()}':"
161161
self.announce(indent + header, level=logging.INFO)
162162
indent = indent + " "
163163
for option, _, _ in self.user_options:
@@ -178,7 +178,7 @@ def run(self):
178178
This method must be implemented by all command classes.
179179
"""
180180
raise RuntimeError(
181-
"abstract method -- subclass %s must override" % self.__class__
181+
f"abstract method -- subclass {self.__class__} must override"
182182
)
183183

184184
def announce(self, msg, level=logging.DEBUG):
@@ -412,7 +412,7 @@ def make_file(
412412
timestamp checks.
413413
"""
414414
if skip_msg is None:
415-
skip_msg = "skipping %s (inputs unchanged)" % outfile
415+
skip_msg = f"skipping {outfile} (inputs unchanged)"
416416

417417
# Allow 'infiles' to be a single string
418418
if isinstance(infiles, str):

distutils/command/bdist.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class bdist(Command):
4141
'plat-name=',
4242
'p',
4343
"platform name to embed in generated filenames "
44-
"(default: %s)" % get_platform(),
44+
f"(default: {get_platform()})",
4545
),
4646
('formats=', None, "formats for distribution (comma-separated list)"),
4747
(
@@ -120,7 +120,7 @@ def finalize_options(self):
120120
except KeyError:
121121
raise DistutilsPlatformError(
122122
"don't know how to create built distributions "
123-
"on platform %s" % os.name
123+
f"on platform {os.name}"
124124
)
125125

126126
if self.dist_dir is None:
@@ -133,7 +133,7 @@ def run(self):
133133
try:
134134
commands.append(self.format_commands[format][0])
135135
except KeyError:
136-
raise DistutilsOptionError("invalid format '%s'" % format)
136+
raise DistutilsOptionError(f"invalid format '{format}'")
137137

138138
# Reinitialize and run each command.
139139
for i in range(len(self.formats)):

distutils/command/bdist_dumb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class bdist_dumb(Command):
2323
'plat-name=',
2424
'p',
2525
"platform name to embed in generated filenames "
26-
"(default: %s)" % get_platform(),
26+
f"(default: {get_platform()})",
2727
),
2828
(
2929
'format=',
@@ -81,7 +81,7 @@ def finalize_options(self):
8181
except KeyError:
8282
raise DistutilsPlatformError(
8383
"don't know how to create dumb built distributions "
84-
"on platform %s" % os.name
84+
f"on platform {os.name}"
8585
)
8686

8787
self.set_undefined_options(

distutils/command/bdist_rpm.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def finalize_options(self):
214214

215215
if os.name != 'posix':
216216
raise DistutilsPlatformError(
217-
"don't know how to create RPM distributions on platform %s" % os.name
217+
f"don't know how to create RPM distributions on platform {os.name}"
218218
)
219219
if self.binary_only and self.source_only:
220220
raise DistutilsOptionError(
@@ -295,9 +295,9 @@ def run(self): # noqa: C901
295295

296296
# Spec file goes into 'dist_dir' if '--spec-only specified',
297297
# build/rpm.<plat> otherwise.
298-
spec_path = os.path.join(spec_dir, "%s.spec" % self.distribution.get_name())
298+
spec_path = os.path.join(spec_dir, f"{self.distribution.get_name()}.spec")
299299
self.execute(
300-
write_file, (spec_path, self._make_spec_file()), "writing '%s'" % spec_path
300+
write_file, (spec_path, self._make_spec_file()), f"writing '{spec_path}'"
301301
)
302302

303303
if self.spec_only: # stop if requested
@@ -322,7 +322,7 @@ def run(self): # noqa: C901
322322
if os.path.exists(self.icon):
323323
self.copy_file(self.icon, source_dir)
324324
else:
325-
raise DistutilsFileError("icon file '%s' does not exist" % self.icon)
325+
raise DistutilsFileError(f"icon file '{self.icon}' does not exist")
326326

327327
# build package
328328
log.info("building RPMs")
@@ -334,9 +334,9 @@ def run(self): # noqa: C901
334334
rpm_cmd.append('-bb')
335335
else:
336336
rpm_cmd.append('-ba')
337-
rpm_cmd.extend(['--define', '__python %s' % self.python])
337+
rpm_cmd.extend(['--define', f'__python {self.python}'])
338338
if self.rpm3_mode:
339-
rpm_cmd.extend(['--define', '_topdir %s' % os.path.abspath(self.rpm_base)])
339+
rpm_cmd.extend(['--define', f'_topdir {os.path.abspath(self.rpm_base)}'])
340340
if not self.keep_temp:
341341
rpm_cmd.append('--clean')
342342

@@ -370,7 +370,7 @@ def run(self): # noqa: C901
370370

371371
status = out.close()
372372
if status:
373-
raise DistutilsExecError("Failed to execute: %r" % q_cmd)
373+
raise DistutilsExecError(f"Failed to execute: {q_cmd!r}")
374374

375375
finally:
376376
out.close()
@@ -426,7 +426,7 @@ def _make_spec_file(self): # noqa: C901
426426
# normalizing the whitespace to simplify the test for whether the
427427
# invocation of brp-python-bytecompile passes in __python):
428428
vendor_hook = '\n'.join([
429-
' %s \\' % line.strip() for line in vendor_hook.splitlines()
429+
f' {line.strip()} \\' for line in vendor_hook.splitlines()
430430
])
431431
problem = "brp-python-bytecompile \\\n"
432432
fixed = "brp-python-bytecompile %{__python} \\\n"
@@ -468,7 +468,7 @@ def _make_spec_file(self): # noqa: C901
468468
if not self.distribution.has_ext_modules():
469469
spec_file.append('BuildArch: noarch')
470470
else:
471-
spec_file.append('BuildArch: %s' % self.force_arch)
471+
spec_file.append(f'BuildArch: {self.force_arch}')
472472

473473
for field in (
474474
'Vendor',
@@ -518,7 +518,7 @@ def _make_spec_file(self): # noqa: C901
518518
# rpm scripts
519519
# figure out default build script
520520
def_setup_call = f"{self.python} {os.path.basename(sys.argv[0])}"
521-
def_build = "%s build" % def_setup_call
521+
def_build = f"{def_setup_call} build"
522522
if self.use_rpm_opt_flags:
523523
def_build = 'env CFLAGS="$RPM_OPT_FLAGS" ' + def_build
524524

@@ -529,8 +529,9 @@ def _make_spec_file(self): # noqa: C901
529529
# are just text that we drop in as-is. Hmmm.
530530

531531
install_cmd = (
532-
'%s install -O1 --root=$RPM_BUILD_ROOT ' '--record=INSTALLED_FILES'
533-
) % def_setup_call
532+
f'{def_setup_call} install -O1 --root=$RPM_BUILD_ROOT '
533+
'--record=INSTALLED_FILES'
534+
)
534535

535536
script_options = [
536537
('prep', 'prep_script', "%setup -n %{name}-%{unmangled_version}"),

distutils/command/build.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class build(Command):
3535
(
3636
'plat-name=',
3737
'p',
38-
"platform name to build for, if supported "
39-
"(default: %s)" % get_platform(),
38+
f"platform name to build for, if supported (default: {get_platform()})",
4039
),
4140
('compiler=', 'c', "specify the compiler type"),
4241
('parallel=', 'j', "number of parallel build jobs"),

0 commit comments

Comments
 (0)