Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim end of name, keeping ext #44

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/hopper_long_name.im
Binary file not shown.
9 changes: 9 additions & 0 deletions Tests/test_file_im.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import filecmp

import pytest
from PIL import Image, ImImagePlugin

Expand All @@ -15,6 +17,13 @@ def test_sanity():
assert im.format == "IM"


def test_name_limit(tmp_path):
out = str(tmp_path / ("name_limit_test" * 7 + ".im"))
with Image.open(TEST_IM) as im:
im.save(out)
assert filecmp.cmp(out, "Tests/images/hopper_long_name.im")


@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file():
def open():
Expand Down
9 changes: 7 additions & 2 deletions src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,14 @@ def _save(im, fp, filename):

fp.write(("Image type: %s image\r\n" % image_type).encode("ascii"))
if filename:
# Each line must be under length 100, or: SyntaxError("not an IM file")
# Each line must be 100 characters or less,
# or: SyntaxError("not an IM file")
# 8 characters are used for "Name: " and "\r\n"
# Keep just the filename, ditch the potentially overlong path
fp.write(("Name: %s\r\n" % os.path.basename(filename)).encode("ascii"))
name, ext = os.path.splitext(os.path.basename(filename))
name = "".join([name[: 92 - len(ext)], ext])

fp.write(("Name: %s\r\n" % name).encode("ascii"))
fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode("ascii"))
fp.write(("File size (no of images): %d\r\n" % frames).encode("ascii"))
if im.mode in ["P", "PA"]:
Expand Down