2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2012-10-25 16:57:39 +04:00
|
|
|
from PIL import Image, TarIO
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
from .helper import is_pypy
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2013-04-21 12:04:58 +04:00
|
|
|
codecs = dir(Image.core)
|
|
|
|
|
2014-09-04 13:21:19 +04:00
|
|
|
# Sample tar archive
|
|
|
|
TEST_TAR_FILE = "Tests/images/hopper.tar"
|
2012-10-25 16:57:39 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_sanity():
|
|
|
|
for codec, test_path, format in [
|
|
|
|
["zip_decoder", "hopper.png", "PNG"],
|
|
|
|
["jpeg_decoder", "hopper.jpg", "JPEG"],
|
|
|
|
]:
|
|
|
|
if codec in codecs:
|
|
|
|
with TarIO.TarIO(TEST_TAR_FILE, test_path) as tar:
|
|
|
|
with Image.open(tar) as im:
|
|
|
|
im.load()
|
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
assert im.format == format
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
|
|
|
|
def test_unclosed_file():
|
|
|
|
def open():
|
|
|
|
TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
|
|
|
|
|
|
|
|
pytest.warns(ResourceWarning, open)
|
|
|
|
|
|
|
|
|
|
|
|
def test_close():
|
|
|
|
def open():
|
|
|
|
tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
pytest.warns(None, open)
|
|
|
|
|
|
|
|
|
|
|
|
def test_contextmanager():
|
|
|
|
def open():
|
|
|
|
with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"):
|
|
|
|
pass
|
|
|
|
|
|
|
|
pytest.warns(None, open)
|