test_imagefile: handle missing PNG & JPEG encoders gracefully.

This commit is contained in:
Michał Górny 2013-04-21 10:13:33 +02:00
parent 6afa11ec02
commit 6cdc8b08db

View File

@ -3,6 +3,8 @@ from tester import *
from PIL import Image from PIL import Image
from PIL import ImageFile from PIL import ImageFile
codecs = dir(Image.core)
# save original block sizes # save original block sizes
MAXBLOCK = ImageFile.MAXBLOCK MAXBLOCK = ImageFile.MAXBLOCK
SAFEBLOCK = ImageFile.SAFEBLOCK SAFEBLOCK = ImageFile.SAFEBLOCK
@ -31,12 +33,13 @@ def test_parser():
assert_image_equal(*roundtrip("GIF")) assert_image_equal(*roundtrip("GIF"))
assert_image_equal(*roundtrip("IM")) assert_image_equal(*roundtrip("IM"))
assert_image_equal(*roundtrip("MSP")) assert_image_equal(*roundtrip("MSP"))
try: if "zip_encoder" in codecs:
# force multiple blocks in PNG driver try:
ImageFile.MAXBLOCK = 8192 # force multiple blocks in PNG driver
assert_image_equal(*roundtrip("PNG")) ImageFile.MAXBLOCK = 8192
finally: assert_image_equal(*roundtrip("PNG"))
ImageFile.MAXBLOCK = MAXBLOCK finally:
ImageFile.MAXBLOCK = MAXBLOCK
assert_image_equal(*roundtrip("PPM")) assert_image_equal(*roundtrip("PPM"))
assert_image_equal(*roundtrip("TIFF")) assert_image_equal(*roundtrip("TIFF"))
assert_image_equal(*roundtrip("XBM")) assert_image_equal(*roundtrip("XBM"))
@ -44,8 +47,9 @@ def test_parser():
assert_image_equal(*roundtrip("TGA")) assert_image_equal(*roundtrip("TGA"))
assert_image_equal(*roundtrip("PCX")) assert_image_equal(*roundtrip("PCX"))
im1, im2 = roundtrip("JPEG") # lossy compression if "jpeg_encoder" in codecs:
assert_image(im1, im2.mode, im2.size) im1, im2 = roundtrip("JPEG") # lossy compression
assert_image(im1, im2.mode, im2.size)
# XXX Why assert exception and why does it fail? # XXX Why assert exception and why does it fail?
# https://github.com/python-imaging/Pillow/issues/78 # https://github.com/python-imaging/Pillow/issues/78
@ -55,6 +59,9 @@ def test_safeblock():
im1 = lena() im1 = lena()
if "zip_encoder" not in codecs:
skip("PNG (zlib) encoder not available")
try: try:
ImageFile.SAFEBLOCK = 1 ImageFile.SAFEBLOCK = 1
im2 = fromstring(tostring(im1, "PNG")) im2 = fromstring(tostring(im1, "PNG"))