Skip to content

Commit b62ff96

Browse files
authored
Add PERF to lint and fix findings (#9510)
2 parents 4b8ae8e + a9ef0e2 commit b62ff96

17 files changed

+50
-69
lines changed

Tests/test_bmp_reference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_questionable() -> None:
5656
im.load()
5757
if os.path.basename(f) not in supported:
5858
print(f"Please add {f} to the partially supported bmp specs.")
59-
except Exception: # as msg:
59+
except Exception: # noqa: PERF203
6060
if os.path.basename(f) in supported:
6161
raise
6262

@@ -106,7 +106,7 @@ def get_compare(f: str) -> str:
106106

107107
assert_image_similar(im_converted, compare_converted, 5)
108108

109-
except Exception as msg:
109+
except Exception as msg: # noqa: PERF203
110110
# there are three here that are unsupported:
111111
unsupported = (
112112
os.path.join(base, "g", "rgb32bf.bmp"),

Tests/test_file_container.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ def test_iter(bytesmode: bool) -> None:
179179
container = ContainerIO.ContainerIO(fh, 0, 120)
180180

181181
# Act
182-
data = []
183-
for line in container:
184-
data.append(line)
182+
data = list(container)
185183

186184
# Assert
187185
if bytesmode:

Tests/test_file_libtiff.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ def test_additional_metadata(
224224
with Image.open("Tests/images/hopper_g4.tif") as im:
225225
assert isinstance(im, TiffImagePlugin.TiffImageFile)
226226
for tag in im.tag_v2:
227-
try:
228-
del core_items[tag]
229-
except KeyError:
230-
pass
227+
core_items.pop(tag, None)
231228
del core_items[320] # colormap is special, tested below
232229

233230
# Type codes:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ lint.select = [
146146
"I", # isort
147147
"ISC", # flake8-implicit-str-concat
148148
"LOG", # flake8-logging
149+
"PERF", # perflint
149150
"PGH", # pygrep-hooks
150151
"PIE", # flake8-pie
151152
"PT", # flake8-pytest-style

setup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def _pkg_config(name: str) -> tuple[list[str], list[str]] | None:
302302
subprocess.check_output(command_cflags).decode("utf8").strip(),
303303
)[::2][1:]
304304
return libs, cflags
305-
except Exception:
305+
except Exception: # noqa: PERF203
306306
pass
307307
return None
308308

@@ -1078,10 +1078,10 @@ def debug_build() -> bool:
10781078
]
10791079

10801080
files: list[str | os.PathLike[str]] = ["src/_imaging.c"]
1081-
for src_file in _IMAGING:
1082-
files.append("src/" + src_file + ".c")
1083-
for src_file in _LIB_IMAGING:
1084-
files.append(os.path.join("src/libImaging", src_file + ".c"))
1081+
files.extend("src/" + src_file + ".c" for src_file in _IMAGING)
1082+
files.extend(
1083+
os.path.join("src/libImaging", src_file + ".c") for src_file in _LIB_IMAGING
1084+
)
10851085
ext_modules = [
10861086
Extension("PIL._imaging", files),
10871087
Extension("PIL._imagingft", ["src/_imagingft.c"]),

src/PIL/GifImagePlugin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ def seek(self, frame: int) -> None:
164164
self._seek(0)
165165

166166
last_frame = self.__frame
167-
for f in range(self.__frame + 1, frame + 1):
168-
try:
167+
try:
168+
for f in range(self.__frame + 1, frame + 1):
169169
self._seek(f)
170-
except EOFError as e:
171-
self.seek(last_frame)
172-
msg = "no more images in GIF file"
173-
raise EOFError(msg) from e
170+
except EOFError as e:
171+
self.seek(last_frame)
172+
msg = "no more images in GIF file"
173+
raise EOFError(msg) from e
174174

175175
def _seek(self, frame: int, update_image: bool = True) -> None:
176176
if isinstance(self._fp, DeferredError):

src/PIL/IcnsImagePlugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ def read_32(
8080
if byte_int & 0x80:
8181
blocksize = byte_int - 125
8282
byte = fobj.read(1)
83-
for i in range(blocksize):
84-
data.append(byte)
83+
data.extend([byte] * blocksize)
8584
else:
8685
blocksize = byte_int + 1
8786
data.append(fobj.read(blocksize))

src/PIL/Image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def init() -> bool:
488488
try:
489489
logger.debug("Importing %s", plugin)
490490
__import__(f"{__spec__.parent}.{plugin}", globals(), locals(), [])
491-
except ImportError as e:
491+
except ImportError as e: # noqa: PERF203
492492
logger.debug("Image: failed to import %s: %s", plugin, e)
493493

494494
if OPEN or SAVE:

src/PIL/ImageDraw.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,7 @@ def draw_text(ink: int, stroke_width: float = 0) -> None:
597597
mode = self.fontmode
598598
if stroke_width == 0 and embedded_color:
599599
mode = "RGBA"
600-
coord = []
601-
for i in range(2):
602-
coord.append(int(xy[i]))
600+
coord = [int(xy[i]) for i in range(2)]
603601
start = (math.modf(xy[0])[0], math.modf(xy[1])[0])
604602
try:
605603
mask, offset = image_text.font.getmask2( # type: ignore[union-attr,misc]

src/PIL/ImageFile.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ def get_child_images(self) -> list[ImageFile]:
215215
if subifd_offsets:
216216
if not isinstance(subifd_offsets, tuple):
217217
subifd_offsets = (subifd_offsets,)
218-
for subifd_offset in subifd_offsets:
219-
ifds.append((exif._get_ifd_dict(subifd_offset), subifd_offset))
218+
ifds = [
219+
(exif._get_ifd_dict(subifd_offset), subifd_offset)
220+
for subifd_offset in subifd_offsets
221+
]
220222
ifd1 = exif.get_ifd(ExifTags.IFD.IFD1)
221223
if ifd1 and ifd1.get(ExifTags.Base.JpegIFOffset):
222224
assert exif._info is not None

0 commit comments

Comments
 (0)