File: test_image_load.py

package info (click to toggle)
pillow 12.1.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 72,624 kB
  • sloc: python: 49,768; ansic: 38,750; makefile: 302; sh: 169; javascript: 85
file content (54 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import annotations

import logging
import os

import pytest

from PIL import Image

from .helper import hopper


def test_sanity() -> None:
    im = hopper()
    px = im.load()

    assert px is not None
    assert px[0, 0] == (20, 20, 70)


def test_close() -> None:
    im = Image.open("Tests/images/hopper.gif")
    im.close()
    with pytest.raises(ValueError):
        im.load()
    with pytest.raises(ValueError):
        im.getpixel((0, 0))


def test_close_after_load(caplog: pytest.LogCaptureFixture) -> None:
    im = Image.open("Tests/images/hopper.gif")
    im.load()
    with caplog.at_level(logging.DEBUG):
        im.close()
    assert len(caplog.records) == 0


def test_contextmanager() -> None:
    fn = None
    with Image.open("Tests/images/hopper.gif") as im:
        assert im.fp is not None
        fn = im.fp.fileno()
        os.fstat(fn)

    with pytest.raises(OSError):
        os.fstat(fn)


def test_contextmanager_non_exclusive_fp() -> None:
    with open("Tests/images/hopper.gif", "rb") as fp:
        with Image.open(fp):
            pass

        assert not fp.closed