Skip to content

Commit

Permalink
move repeated code to private helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Yay295 committed Aug 18, 2024
1 parent 35a70e4 commit cd76b48
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2080,38 +2080,34 @@ def readLong(self) -> int:
(value,) = struct.unpack(self.longFmt, self.f.read(4))
return value

@staticmethod
def _verify_bytes_written(bytes_written: int | None, expected: int) -> None:
if bytes_written is not None and bytes_written != expected:
msg = f"wrote only {bytes_written} bytes but wanted {expected}"
raise RuntimeError(msg)

def rewriteLastShortToLong(self, value: int) -> None:
self.f.seek(-2, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.longFmt, value))
if bytes_written is not None and bytes_written != 4:
msg = f"wrote only {bytes_written} bytes but wanted 4"
raise RuntimeError(msg)
self._verify_bytes_written(bytes_written, 4)

Check warning on line 2092 in src/PIL/TiffImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/TiffImagePlugin.py#L2092

Added line #L2092 was not covered by tests

def rewriteLastShort(self, value: int) -> None:
self.f.seek(-2, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.shortFmt, value))
if bytes_written is not None and bytes_written != 2:
msg = f"wrote only {bytes_written} bytes but wanted 2"
raise RuntimeError(msg)
self._verify_bytes_written(bytes_written, 2)

Check warning on line 2097 in src/PIL/TiffImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/TiffImagePlugin.py#L2097

Added line #L2097 was not covered by tests

def rewriteLastLong(self, value: int) -> None:
self.f.seek(-4, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.longFmt, value))
if bytes_written is not None and bytes_written != 4:
msg = f"wrote only {bytes_written} bytes but wanted 4"
raise RuntimeError(msg)
self._verify_bytes_written(bytes_written, 4)

Check warning on line 2102 in src/PIL/TiffImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/TiffImagePlugin.py#L2102

Added line #L2102 was not covered by tests

def writeShort(self, value: int) -> None:
bytes_written = self.f.write(struct.pack(self.shortFmt, value))
if bytes_written is not None and bytes_written != 2:
msg = f"wrote only {bytes_written} bytes but wanted 2"
raise RuntimeError(msg)
self._verify_bytes_written(bytes_written, 2)

def writeLong(self, value: int) -> None:
bytes_written = self.f.write(struct.pack(self.longFmt, value))
if bytes_written is not None and bytes_written != 4:
msg = f"wrote only {bytes_written} bytes but wanted 4"
raise RuntimeError(msg)
self._verify_bytes_written(bytes_written, 4)

def close(self) -> None:
self.finalize()
Expand Down

0 comments on commit cd76b48

Please sign in to comment.