Skip to content

Commit

Permalink
Merge pull request #94 from boidolr/fix/close-image
Browse files Browse the repository at this point in the history
Fix: avoid potential leaks
  • Loading branch information
boidolr committed Mar 25, 2024
2 parents 5c3079a + 75ee8cb commit 2e6b885
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 49 deletions.
4 changes: 2 additions & 2 deletions pre_commit_images/optimize_avif.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def main(argv: Sequence[str] | None = None) -> int:
args = parser.parse_args(argv)

def optimize(source: Path, target: IO[bytes]) -> None:
im = Image.open(source)
im.save(target, format=im.format, speed=args.speed, quality=args.quality)
with Image.open(source) as im:
im.save(target, format=im.format, speed=args.speed, quality=args.quality)

success = _optimize_images(args.filenames, optimize, args.threshold)
return 0 if success else 1
Expand Down
4 changes: 2 additions & 2 deletions pre_commit_images/optimize_jpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def main(argv: Sequence[str] | None = None) -> int:
args = parser.parse_args(argv)

def optimize(source: Path, target: IO[bytes]) -> None:
im = Image.open(source)
im.save(target, format=im.format, optimize=True, progressive=True, quality=args.quality)
with Image.open(source) as im:
im.save(target, format=im.format, optimize=True, progressive=True, quality=args.quality)

success = _optimize_images(args.filenames, optimize, args.threshold)
return 0 if success else 1
Expand Down
4 changes: 2 additions & 2 deletions pre_commit_images/optimize_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def main(argv: Sequence[str] | None = None) -> int:
args = parser.parse_args(argv)

def optimize(source: Path, target: IO[bytes]) -> None:
im = Image.open(source)
im.save(target, format=im.format, optimize=True)
with Image.open(source) as im:
im.save(target, format=im.format, optimize=True)

success = _optimize_images(args.filenames, optimize, args.threshold)
return 0 if success else 1
Expand Down
16 changes: 8 additions & 8 deletions pre_commit_images/optimize_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ def main(argv: Sequence[str] | None = None) -> int:
args = parser.parse_args(argv)

def optimize(source: Path, target: IO[bytes]) -> None:
im = Image.open(source)
im.save(
target,
format=im.format,
lossless=args.lossless,
method=6,
quality=args.quality,
)
with Image.open(source) as im:
im.save(
target,
format=im.format,
lossless=args.lossless,
method=6,
quality=args.quality,
)

success = _optimize_images(args.filenames, optimize, args.threshold)
return 0 if success else 1
Expand Down
52 changes: 17 additions & 35 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,23 @@ fix = true

[tool.ruff.lint]
select = [
# Pyflakes
"F",
# Pycodestyle
"E",
"W",
# pyupgrade
"UP",
# refurb
"FURB",
# flake8-bandit
"S",
# flake8-comprehensions
"C4",
# flake8-logging-format
"G",
# flake8-annotations
"ANN",
# flake8-simplify
"SIM",
# flake8-pytest-style
"PT",
# flake8-return
"RET",
# flake8-use-pathlib
"PTH",
# flake8-tidy-imports (relative-imports)
"TID252",
# Perflint
"PERF",
# flynt
"FLY",
# isort
"I001",
# Unneeded noqa
"RUF100"
"F", # Pyflakes
"E", "W", # Pycodestyle
"UP", # pyupgrade
"FURB", # refurb
"S", # flake8-bandit
"C4", # flake8-comprehensions
"G", # flake8-logging-format
"ANN", # flake8-annotations
"SIM", # flake8-simplify
"PT", # flake8-pytest-style
"RET", # flake8-return
"PTH", # flake8-use-pathlib
"TID252", # flake8-tidy-imports (relative-imports)
"PERF", # Perflint
"FLY", # flynt
"I", # isort
"RUF100" # Unneeded noqa
]

[tool.ruff.lint.per-file-ignores]
Expand Down

0 comments on commit 2e6b885

Please sign in to comment.