mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-13 18:56:17 +03:00
Merge pull request #44 from radarhere/fix-im-long-name
Trim end of name, keeping ext
This commit is contained in:
commit
853e2f25be
BIN
Tests/images/hopper_long_name.im
Normal file
BIN
Tests/images/hopper_long_name.im
Normal file
Binary file not shown.
|
@ -1,3 +1,5 @@
|
||||||
|
import filecmp
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from PIL import Image, ImImagePlugin
|
from PIL import Image, ImImagePlugin
|
||||||
|
|
||||||
|
@ -15,6 +17,13 @@ def test_sanity():
|
||||||
assert im.format == "IM"
|
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")
|
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
|
||||||
def test_unclosed_file():
|
def test_unclosed_file():
|
||||||
def open():
|
def open():
|
||||||
|
|
|
@ -348,9 +348,14 @@ def _save(im, fp, filename):
|
||||||
|
|
||||||
fp.write(("Image type: %s image\r\n" % image_type).encode("ascii"))
|
fp.write(("Image type: %s image\r\n" % image_type).encode("ascii"))
|
||||||
if filename:
|
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
|
# 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(("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"))
|
fp.write(("File size (no of images): %d\r\n" % frames).encode("ascii"))
|
||||||
if im.mode in ["P", "PA"]:
|
if im.mode in ["P", "PA"]:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user