Skip to content

Commit

Permalink
Merge pull request #8043 from mrKazzila/tmp/pr/move_to_f_str
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere authored May 5, 2024
2 parents 58a4797 + 71b8d99 commit 93ca52f
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 40 deletions.
8 changes: 4 additions & 4 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,16 @@ class D3DFMT(IntEnum):
module = sys.modules[__name__]
for item in DDSD:
assert item.name is not None
setattr(module, "DDSD_" + item.name, item.value)
setattr(module, f"DDSD_{item.name}", item.value)
for item1 in DDSCAPS:
assert item1.name is not None
setattr(module, "DDSCAPS_" + item1.name, item1.value)
setattr(module, f"DDSCAPS_{item1.name}", item1.value)
for item2 in DDSCAPS2:
assert item2.name is not None
setattr(module, "DDSCAPS2_" + item2.name, item2.value)
setattr(module, f"DDSCAPS2_{item2.name}", item2.value)
for item3 in DDPF:
assert item3.name is not None
setattr(module, "DDPF_" + item3.name, item3.value)
setattr(module, f"DDPF_{item3.name}", item3.value)

DDS_FOURCC = DDPF.FOURCC
DDS_RGB = DDPF.RGB
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _open(self):
n += 1

else:
msg = "Syntax error in IM header: " + s.decode("ascii", "replace")
msg = f"Syntax error in IM header: {s.decode('ascii', 'replace')}"
raise SyntaxError(msg)

if not n:
Expand Down
14 changes: 7 additions & 7 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def _getdecoder(mode, decoder_name, args, extra=()):

try:
# get decoder
decoder = getattr(core, decoder_name + "_decoder")
decoder = getattr(core, f"{decoder_name}_decoder")
except AttributeError as e:
msg = f"decoder {decoder_name} not available"
raise OSError(msg) from e
Expand All @@ -428,7 +428,7 @@ def _getencoder(mode, encoder_name, args, extra=()):

try:
# get encoder
encoder = getattr(core, encoder_name + "_encoder")
encoder = getattr(core, f"{encoder_name}_encoder")
except AttributeError as e:
msg = f"encoder {encoder_name} not available"
raise OSError(msg) from e
Expand Down Expand Up @@ -603,7 +603,7 @@ def _dump(
) -> str:
suffix = ""
if format:
suffix = "." + format
suffix = f".{format}"

if not file:
f, filename = tempfile.mkstemp(suffix)
Expand Down Expand Up @@ -2180,7 +2180,7 @@ def resize(self, size, resample=None, box=None, reducing_gap=None) -> Image:
(Resampling.HAMMING, "Image.Resampling.HAMMING"),
)
]
msg += " Use " + ", ".join(filters[:-1]) + " or " + filters[-1]
msg += f" Use {', '.join(filters[:-1])} or {filters[-1]}"
raise ValueError(msg)

if reducing_gap is not None and reducing_gap < 1.0:
Expand Down Expand Up @@ -2825,7 +2825,7 @@ def __transformer(
(Resampling.BICUBIC, "Image.Resampling.BICUBIC"),
)
]
msg += " Use " + ", ".join(filters[:-1]) + " or " + filters[-1]
msg += f" Use {', '.join(filters[:-1])} or {filters[-1]}"
raise ValueError(msg)

image.load()
Expand Down Expand Up @@ -3223,8 +3223,8 @@ def fromqpixmap(im):
((1, 1, 3), "|u1"): ("RGB", "RGB"),
((1, 1, 4), "|u1"): ("RGBA", "RGBA"),
# shortcuts:
((1, 1), _ENDIAN + "i4"): ("I", "I"),
((1, 1), _ENDIAN + "f4"): ("F", "F"),
((1, 1), f"{_ENDIAN}i4"): ("I", "I"),
((1, 1), f"{_ENDIAN}f4"): ("F", "F"),
}


Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def apply(
out = Image.new(mode or im_1.mode, im_1.size, None)
im_1.load()
try:
op = getattr(_imagingmath, op + "_" + im_1.mode)
op = getattr(_imagingmath, f"{op}_{im_1.mode}")
except AttributeError as e:
msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e
Expand Down Expand Up @@ -89,7 +89,7 @@ def apply(
im_1.load()
im_2.load()
try:
op = getattr(_imagingmath, op + "_" + im_1.mode)
op = getattr(_imagingmath, f"{op}_{im_1.mode}")
except AttributeError as e:
msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e
Expand Down
8 changes: 4 additions & 4 deletions src/PIL/ImageMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def getmode(mode: str) -> ModeDescriptor:
# Bits need to be extended to bytes
"1": ("L", "L", ("1",), "|b1"),
"L": ("L", "L", ("L",), "|u1"),
"I": ("L", "I", ("I",), endian + "i4"),
"F": ("L", "F", ("F",), endian + "f4"),
"I": ("L", "I", ("I",), f"{endian}i4"),
"F": ("L", "F", ("F",), f"{endian}f4"),
"P": ("P", "L", ("P",), "|u1"),
"RGB": ("RGB", "L", ("R", "G", "B"), "|u1"),
"RGBX": ("RGB", "L", ("R", "G", "B", "X"), "|u1"),
Expand Down Expand Up @@ -78,8 +78,8 @@ def getmode(mode: str) -> ModeDescriptor:
"I;16LS": "<i2",
"I;16B": ">u2",
"I;16BS": ">i2",
"I;16N": endian + "u2",
"I;16NS": endian + "i2",
"I;16N": f"{endian}u2",
"I;16NS": f"{endian}i2",
"I;32": "<u4",
"I;32B": ">u4",
"I;32L": "<u4",
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageMorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(
],
}
if op_name not in known_patterns:
msg = "Unknown pattern " + op_name + "!"
msg = f"Unknown pattern {op_name}!"
raise Exception(msg)

self.patterns = known_patterns[op_name]
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __init__(self, title="PIL", width=None, height=None):
)

def __dispatcher(self, action, *args):
return getattr(self, "ui_handle_" + action)(*args)
return getattr(self, f"ui_handle_{action}")(*args)

def ui_handle_clear(self, dc, x0, y0, x1, y1):
pass
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PalmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _save(im, fp, filename):

# we ignore the palette here
im.mode = "P"
rawmode = "P;" + str(bpp)
rawmode = f"P;{bpp}"
version = 1

elif im.mode == "1":
Expand Down
8 changes: 3 additions & 5 deletions src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ def __delitem__(self, key):
elif key in self.deleted_entries:
generation = self.deleted_entries[key]
else:
msg = (
"object ID " + str(key) + " cannot be deleted because it doesn't exist"
)
msg = f"object ID {key} cannot be deleted because it doesn't exist"
raise IndexError(msg)

def __contains__(self, key):
Expand Down Expand Up @@ -225,7 +223,7 @@ def __hash__(self):
return hash(self.name)

def __repr__(self):
return f"PdfName({repr(self.name)})"
return f"{self.__class__.__name__}({repr(self.name)})"

@classmethod
def from_pdf_stream(cls, data):
Expand Down Expand Up @@ -884,7 +882,7 @@ def get_value(cls, data, offset, expect_indirect=None, max_nesting=-1):
if m:
return cls.get_literal_string(data, m.end())
# return None, offset # fallback (only for debugging)
msg = "unrecognized object: " + repr(data[offset : offset + 32])
msg = f"unrecognized object: {repr(data[offset : offset + 32])}"
raise PdfFormatError(msg)

re_lit_str_token = re.compile(
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def call(self, cid, pos, length):
"""Call the appropriate chunk handler"""

logger.debug("STREAM %r %s %s", cid, pos, length)
return getattr(self, "chunk_" + cid.decode("ascii"))(pos, length)
return getattr(self, f"chunk_{cid.decode('ascii')}")(pos, length)

def crc(self, cid, data):
"""Read and verify checksum"""
Expand Down
10 changes: 5 additions & 5 deletions src/PIL/SpiderImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def loadImageSeries(filelist=None):
im = im.convert2byte()
except Exception:
if not isSpiderImage(img):
print(img + " is not a Spider image file")
print(f"{img} is not a Spider image file")
continue
im.info["filename"] = img
imglist.append(im)
Expand Down Expand Up @@ -299,10 +299,10 @@ def _save_spider(im, fp, filename):
sys.exit()

with Image.open(filename) as im:
print("image: " + str(im))
print("format: " + str(im.format))
print("size: " + str(im.size))
print("mode: " + str(im.mode))
print(f"image: {im}")
print(f"format: {im.format}")
print(f"size: {im.size}")
print(f"mode: {im.mode}")
print("max, min: ", end=" ")
print(im.getextrema())

Expand Down
12 changes: 6 additions & 6 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def _register_basic(idx_fmt_name):

idx, fmt, name = idx_fmt_name
TYPES[idx] = name
size = struct.calcsize("=" + fmt)
size = struct.calcsize(f"={fmt}")
_load_dispatch[idx] = ( # noqa: F821
size,
lambda self, data, legacy_api=True: (
Expand Down Expand Up @@ -987,8 +987,8 @@ def save(self, fp):
ImageFileDirectory_v2._write_dispatch = _write_dispatch
for idx, name in TYPES.items():
name = name.replace(" ", "_")
setattr(ImageFileDirectory_v2, "load_" + name, _load_dispatch[idx][1])
setattr(ImageFileDirectory_v2, "write_" + name, _write_dispatch[idx])
setattr(ImageFileDirectory_v2, f"load_{name}", _load_dispatch[idx][1])
setattr(ImageFileDirectory_v2, f"write_{name}", _write_dispatch[idx])
del _load_dispatch, _write_dispatch, idx, name


Expand Down Expand Up @@ -2025,9 +2025,9 @@ def goToEnd(self):

def setEndian(self, endian):
self.endian = endian
self.longFmt = self.endian + "L"
self.shortFmt = self.endian + "H"
self.tagFormat = self.endian + "HHL"
self.longFmt = f"{self.endian}L"
self.shortFmt = f"{self.endian}H"
self.tagFormat = f"{self.endian}HHL"

def skipIFDs(self):
while True:
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def check_codec(feature):

codec, lib = codecs[feature]

return codec + "_encoder" in dir(Image.core)
return f"{codec}_encoder" in dir(Image.core)


def version_codec(feature):
Expand All @@ -105,7 +105,7 @@ def version_codec(feature):

codec, lib = codecs[feature]

version = getattr(Image.core, lib + "_version")
version = getattr(Image.core, f"{lib}_version")

if feature == "libtiff":
return version.split("\n")[0].split("Version ")[1]
Expand Down

0 comments on commit 93ca52f

Please sign in to comment.