Use f-strings

This commit is contained in:
Andrew Murray 2025-06-24 18:55:34 +10:00
parent 2954964cd2
commit f6c350d5d1

View File

@ -16,7 +16,6 @@ import subprocess
import sys import sys
import warnings import warnings
from collections.abc import Iterator from collections.abc import Iterator
from typing import Any
from setuptools import Extension, setup from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext from setuptools.command.build_ext import build_ext
@ -148,11 +147,8 @@ class RequiredDependencyException(Exception):
PLATFORM_MINGW = os.name == "nt" and "GCC" in sys.version PLATFORM_MINGW = os.name == "nt" and "GCC" in sys.version
def _dbg(s: str, tp: Any = None) -> None: def _dbg(s: str) -> None:
if DEBUG: if DEBUG:
if tp:
print(s % tp)
return
print(s) print(s)
@ -214,10 +210,10 @@ def _add_directory(
subdir = os.path.realpath(subdir) subdir = os.path.realpath(subdir)
if os.path.isdir(subdir) and subdir not in path: if os.path.isdir(subdir) and subdir not in path:
if where is None: if where is None:
_dbg("Appending path %s", subdir) _dbg(f"Appending path {subdir}")
path.append(subdir) path.append(subdir)
else: else:
_dbg("Inserting path %s", subdir) _dbg(f"Inserting path {subdir}")
path.insert(where, subdir) path.insert(where, subdir)
elif subdir in path and where is not None: elif subdir in path and where is not None:
path.remove(subdir) path.remove(subdir)
@ -226,10 +222,10 @@ def _add_directory(
def _find_include_file(self: pil_build_ext, include: str) -> str | None: def _find_include_file(self: pil_build_ext, include: str) -> str | None:
for directory in self.compiler.include_dirs: for directory in self.compiler.include_dirs:
_dbg("Checking for include file %s in %s", (include, directory)) _dbg(f"Checking for include file {include} in {directory}")
path = os.path.join(directory, include) path = os.path.join(directory, include)
if os.path.isfile(path): if os.path.isfile(path):
_dbg("Found %s", include) _dbg(f"Found {include}")
return path return path
return None return None
@ -237,22 +233,22 @@ def _find_include_file(self: pil_build_ext, include: str) -> str | None:
def _find_library_file(self: pil_build_ext, library: str) -> str | None: def _find_library_file(self: pil_build_ext, library: str) -> str | None:
ret = self.compiler.find_library_file(self.compiler.library_dirs, library) ret = self.compiler.find_library_file(self.compiler.library_dirs, library)
if ret: if ret:
_dbg("Found library %s at %s", (library, ret)) _dbg(f"Found library {library} at {ret}")
else: else:
_dbg("Couldn't find library %s in %s", (library, self.compiler.library_dirs)) _dbg(f"Couldn't find library {library} in {self.compiler.library_dirs}")
return ret return ret
def _find_include_dir(self: pil_build_ext, dirname: str, include: str) -> bool | str: def _find_include_dir(self: pil_build_ext, dirname: str, include: str) -> bool | str:
for directory in self.compiler.include_dirs: for directory in self.compiler.include_dirs:
_dbg("Checking for include file %s in %s", (include, directory)) _dbg(f"Checking for include file {include} in {directory}")
if os.path.isfile(os.path.join(directory, include)): if os.path.isfile(os.path.join(directory, include)):
_dbg("Found %s in %s", (include, directory)) _dbg(f"Found {include} in {directory}")
return True return True
subdir = os.path.join(directory, dirname) subdir = os.path.join(directory, dirname)
_dbg("Checking for include file %s in %s", (include, subdir)) _dbg(f"Checking for include file {include} in {subdir}")
if os.path.isfile(os.path.join(subdir, include)): if os.path.isfile(os.path.join(subdir, include)):
_dbg("Found %s in %s", (include, subdir)) _dbg(f"Found {include} in {subdir}")
return subdir return subdir
return False return False
@ -396,7 +392,7 @@ class pil_build_ext(build_ext):
if getattr(self, f"disable_{x}"): if getattr(self, f"disable_{x}"):
self.feature.set(x, False) self.feature.set(x, False)
self.feature.required.discard(x) self.feature.required.discard(x)
_dbg("Disabling %s", x) _dbg(f"Disabling {x}")
if getattr(self, f"enable_{x}"): if getattr(self, f"enable_{x}"):
msg = f"Conflicting options: '-C {x}=enable' and '-C {x}=disable'" msg = f"Conflicting options: '-C {x}=enable' and '-C {x}=disable'"
raise ValueError(msg) raise ValueError(msg)
@ -410,7 +406,7 @@ class pil_build_ext(build_ext):
raise ValueError(msg) raise ValueError(msg)
setattr(self, "disable_raqm", True) setattr(self, "disable_raqm", True)
if getattr(self, f"enable_{x}"): if getattr(self, f"enable_{x}"):
_dbg("Requiring %s", x) _dbg(f"Requiring {x}")
self.feature.required.add(x) self.feature.required.add(x)
if x == "raqm": if x == "raqm":
_dbg("'-C raqm=enable' implies '-C freetype=enable'") _dbg("'-C raqm=enable' implies '-C freetype=enable'")
@ -425,7 +421,7 @@ class pil_build_ext(build_ext):
f"Conflicting options: '-C {x}=vendor' and not '-C raqm=vendor'" f"Conflicting options: '-C {x}=vendor' and not '-C raqm=vendor'"
) )
raise ValueError(msg) raise ValueError(msg)
_dbg("Using vendored version of %s", x) _dbg(f"Using vendored version of {x}")
self.feature.vendor.add(x) self.feature.vendor.add(x)
def _update_extension( def _update_extension(
@ -673,7 +669,7 @@ class pil_build_ext(build_ext):
best_path = os.path.join(program_files, name) best_path = os.path.join(program_files, name)
if best_path: if best_path:
_dbg("Adding %s to search list", best_path) _dbg(f"Adding {best_path} to search list")
_add_directory(library_dirs, os.path.join(best_path, "lib")) _add_directory(library_dirs, os.path.join(best_path, "lib"))
_add_directory(include_dirs, os.path.join(best_path, "include")) _add_directory(include_dirs, os.path.join(best_path, "include"))
@ -715,7 +711,7 @@ class pil_build_ext(build_ext):
# Find the best version # Find the best version
for directory in self.compiler.include_dirs: for directory in self.compiler.include_dirs:
_dbg("Checking for openjpeg-#.# in %s", directory) _dbg(f"Checking for openjpeg-#.# in {directory}")
try: try:
listdir = os.listdir(directory) listdir = os.listdir(directory)
except Exception: except Exception:
@ -725,14 +721,14 @@ class pil_build_ext(build_ext):
if name.startswith("openjpeg-") and os.path.isfile( if name.startswith("openjpeg-") and os.path.isfile(
os.path.join(directory, name, "openjpeg.h") os.path.join(directory, name, "openjpeg.h")
): ):
_dbg("Found openjpeg.h in %s/%s", (directory, name)) _dbg(f"Found openjpeg.h in {directory}/{name}")
version = tuple(int(x) for x in name[9:].split(".")) version = tuple(int(x) for x in name[9:].split("."))
if best_version is None or version > best_version: if best_version is None or version > best_version:
best_version = version best_version = version
best_path = os.path.join(directory, name) best_path = os.path.join(directory, name)
_dbg( _dbg(
"Best openjpeg version %s so far in %s", f"Best openjpeg version {best_version} "
(best_version, best_path), f"so far in {best_path}"
) )
if best_version and _find_library_file(self, "openjp2"): if best_version and _find_library_file(self, "openjp2"):
@ -767,16 +763,16 @@ class pil_build_ext(build_ext):
# look for freetype2 include files # look for freetype2 include files
freetype_version = 0 freetype_version = 0
for subdir in self.compiler.include_dirs: for subdir in self.compiler.include_dirs:
_dbg("Checking for include file %s in %s", ("ft2build.h", subdir)) _dbg(f"Checking for include file ft2build.h in {subdir}")
if os.path.isfile(os.path.join(subdir, "ft2build.h")): if os.path.isfile(os.path.join(subdir, "ft2build.h")):
_dbg("Found %s in %s", ("ft2build.h", subdir)) _dbg(f"Found ft2build.h in {subdir}")
freetype_version = 21 freetype_version = 21
subdir = os.path.join(subdir, "freetype2") subdir = os.path.join(subdir, "freetype2")
break break
subdir = os.path.join(subdir, "freetype2") subdir = os.path.join(subdir, "freetype2")
_dbg("Checking for include file %s in %s", ("ft2build.h", subdir)) _dbg(f"Checking for include file ft2build.h in {subdir}")
if os.path.isfile(os.path.join(subdir, "ft2build.h")): if os.path.isfile(os.path.join(subdir, "ft2build.h")):
_dbg("Found %s in %s", ("ft2build.h", subdir)) _dbg(f"Found ft2build.h in {subdir}")
freetype_version = 21 freetype_version = 21
break break
if freetype_version: if freetype_version: