Skip to content

Commit

Permalink
Assign exception string to variable first
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 24, 2023
1 parent f73325f commit 0ed7d5c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/PIL/VtfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ def _get_texture_size(pixel_format: VtfPF, width, height):
return width * height * 3
elif pixel_format == VtfPF.RGBA8888:
return width * height * 4
raise VTFException(f"Unsupported VTF pixel format: {pixel_format}")
msg = f"Unsupported VTF pixel format: {pixel_format}"
raise VTFException(msg)

Check warning on line 148 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L147-L148

Added lines #L147 - L148 were not covered by tests


def _get_mipmap_count(width: int, height: int):
Expand Down Expand Up @@ -196,7 +197,8 @@ def _write_image(fp: BufferedIOBase, im: Image.Image, pixel_format: VtfPF):
encoder = "raw"
encoder_args = ("RG", 0, 0)
else:
raise VTFException(f"Unsupported pixel format: {pixel_format!r}")
msg = f"Unsupported pixel format: {pixel_format!r}"
raise VTFException(msg)

Check warning on line 201 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L200-L201

Added lines #L200 - L201 were not covered by tests

tile = [(encoder, extents, fp.tell(), encoder_args)]
ImageFile._save(im, fp, tile, _get_texture_size(pixel_format, *im.size))
Expand All @@ -213,7 +215,8 @@ class VtfImageFile(ImageFile.ImageFile):

def _open(self):
if not _accept(self.fp.read(12)):
raise SyntaxError("not a VTF file")
msg = "not a VTF file"
raise SyntaxError(msg)

Check warning on line 219 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L218-L219

Added lines #L218 - L219 were not covered by tests
self.fp.seek(4)
version = struct.unpack("<2I", self.fp.read(8))
if version <= (7, 2):
Expand Down Expand Up @@ -241,7 +244,8 @@ def _open(self):
)
self.fp.seek(header.header_size)
else:
raise VTFException(f"Unsupported VTF version: {version}")
msg = f"Unsupported VTF version: {version}"
raise VTFException(msg)

Check warning on line 248 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L247-L248

Added lines #L247 - L248 were not covered by tests
# flags = CompiledVtfFlags(header.flags)
pixel_format = VtfPF(header.pixel_format)
low_format = VtfPF(header.low_pixel_format)
Expand All @@ -262,7 +266,8 @@ def _open(self):
elif pixel_format == VtfPF.IA88:
self._mode = "LA"
else:
raise VTFException(f"Unsupported VTF pixel format: {pixel_format}")
msg = f"Unsupported VTF pixel format: {pixel_format}"
raise VTFException(msg)

Check warning on line 270 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L269-L270

Added lines #L269 - L270 were not covered by tests

self._size = (header.width, header.height)

Expand Down Expand Up @@ -298,14 +303,16 @@ def _open(self):
elif pixel_format == VtfPF.IA88:
tile = ("raw", (0, 0) + self.size, data_start, ("LA", 0, 1))
else:
raise VTFException(f"Unsupported VTF pixel format: {pixel_format}")
msg = f"Unsupported VTF pixel format: {pixel_format}"
raise VTFException(msg)

Check warning on line 307 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L306-L307

Added lines #L306 - L307 were not covered by tests
self.tile = [tile]


def _save(im, fp, filename):
im: Image.Image
if im.mode not in ("RGB", "RGBA", "L", "LA"):
raise OSError(f"cannot write mode {im.mode} as VTF")
msg = f"cannot write mode {im.mode} as VTF"
raise OSError(msg)

Check warning on line 315 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L314-L315

Added lines #L314 - L315 were not covered by tests
encoderinfo = im.encoderinfo
pixel_format = VtfPF(encoderinfo.get("pixel_format", VtfPF.RGBA8888))
version = encoderinfo.get("version", (7, 4))
Expand Down Expand Up @@ -373,7 +380,8 @@ def _save(im, fp, filename):
header = header._replace(header_size=size + (16 - size % 16))
fp.write(struct.pack(HEADER_V73, *header))
else:
raise VTFException(f"Unsupported version {version}")
msg = f"Unsupported version {version}"
raise VTFException(msg)

Check warning on line 384 in src/PIL/VtfImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/VtfImagePlugin.py#L383-L384

Added lines #L383 - L384 were not covered by tests

if version > (7, 2):
fp.write(b"\x01\x00\x00\x00")
Expand Down

0 comments on commit 0ed7d5c

Please sign in to comment.