2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-07-29 01:13:56 +03:00
|
|
|
import logging
|
2019-07-06 23:40:53 +03:00
|
|
|
import os
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
from .helper import hopper
|
2014-04-18 09:01:55 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2020-01-27 14:46:52 +03:00
|
|
|
im = hopper()
|
2024-07-02 13:10:47 +03:00
|
|
|
px = im.load()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2024-07-02 13:10:47 +03:00
|
|
|
assert px is not None
|
|
|
|
assert px[0, 0] == (20, 20, 70)
|
2014-04-18 09:01:55 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_close() -> None:
|
2020-01-27 14:46:52 +03:00
|
|
|
im = Image.open("Tests/images/hopper.gif")
|
|
|
|
im.close()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.load()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.getpixel((0, 0))
|
2014-04-18 09:01:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_close_after_load(caplog: pytest.LogCaptureFixture) -> None:
|
2020-07-29 01:13:56 +03:00
|
|
|
im = Image.open("Tests/images/hopper.gif")
|
|
|
|
im.load()
|
|
|
|
with caplog.at_level(logging.DEBUG):
|
|
|
|
im.close()
|
|
|
|
assert len(caplog.records) == 0
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_contextmanager() -> None:
|
2020-01-27 14:46:52 +03:00
|
|
|
fn = None
|
|
|
|
with Image.open("Tests/images/hopper.gif") as im:
|
|
|
|
fn = im.fp.fileno()
|
|
|
|
os.fstat(fn)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
with pytest.raises(OSError):
|
|
|
|
os.fstat(fn)
|
2019-03-06 13:55:32 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_contextmanager_non_exclusive_fp() -> None:
|
2020-01-27 14:46:52 +03:00
|
|
|
with open("Tests/images/hopper.gif", "rb") as fp:
|
|
|
|
with Image.open(fp):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert not fp.closed
|