2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-02-21 05:49:01 +03:00
|
|
|
import warnings
|
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
from PIL import Image, TarIO, features
|
2012-10-25 16:57:39 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
from .helper import is_pypy
|
2019-07-06 23:40:53 +03:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2023-01-16 02:32:58 +03:00
|
|
|
@pytest.mark.parametrize(
|
2023-01-16 04:36:52 +03:00
|
|
|
"codec, test_path, format",
|
2023-01-16 02:32:58 +03:00
|
|
|
(
|
|
|
|
("zlib", "hopper.png", "PNG"),
|
|
|
|
("jpg", "hopper.jpg", "JPEG"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_sanity(codec, test_path, format):
|
|
|
|
if features.check(codec):
|
|
|
|
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
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
|
|
|
|
def test_unclosed_file():
|
2023-02-23 16:30:38 +03:00
|
|
|
with pytest.warns(ResourceWarning):
|
2020-02-12 19:29:19 +03:00
|
|
|
TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
|
|
|
|
|
|
|
|
|
|
|
|
def test_close():
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2020-02-12 19:29:19 +03:00
|
|
|
tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
|
|
|
|
def test_contextmanager():
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2020-02-12 19:29:19 +03:00
|
|
|
with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"):
|
|
|
|
pass
|