Merge branch 'main' into duru

This commit is contained in:
dutcu 2024-06-20 10:37:18 +02:00
commit bdd7bf1d87
2 changed files with 90 additions and 27 deletions

View File

@ -1,27 +1,26 @@
# .coveragerc to control coverage.py # .coveragerc to control coverage.py
[report] [report]
# Regexes for lines to exclude from consideration # Regexes for lines to exclude from consideration
exclude_also = exclude_also =
# Don't complain if non-runnable code isn't run # Don't complain if non-runnable code isn't run
if 0: if 0:
if __name__ == .__main__.: if _name_ == ._main_.:
# Don't complain about debug code # Don't complain about debug code
if DEBUG: if DEBUG:
# Don't complain about compatibility code for missing optional dependencies # Don't complain about compatibility code for missing optional dependencies
except ImportError except ImportError
if TYPE_CHECKING: if TYPE_CHECKING:
@abc.abstractmethod @abc.abstractmethod
# Empty bodies in protocols or abstract methods # Empty bodies in protocols or abstract methods
^\s*def [a-zA-Z0-9_]+\(.*\)(\s*->.*)?:\s*\.\.\.(\s*#.*)?$ ^\s*def [a-zA-Z0-9_]+\(.\)(\s->.)?:\s\.\.\.(\s*#.*)?$
^\s*\.\.\.(\s*#.*)?$ ^\s*\.\.\.(\s*#.*)?$
[run] [run]
omit = omit =
# Tests/32bit_segfault_check.py # Tests/32bit_segfault_check.py
# Tests/bench_cffi_access.py # Tests/bench_cffi_access.py
# Tests/check_*.py # Tests/check_*.py
# Tests/createfontdatachunk.py # Tests/createfontdatachunk.py
Tests/* Tests/*
src/* src/*

View File

@ -0,0 +1,64 @@
import unittest
from PIL import Image
import unittest
class TestFromBytes(unittest.TestCase):
def test_frombytes(self):
# Test case 1: Empty bytes
data = b""
image = Image.frombytes("RGB", (0, 0), data)
self.assertEqual(image.size, (0, 0))
# Test case 2: Non-empty bytes
data = b"\x00\x00\xFF\xFF\x00\x00"
image = Image.frombytes("RGB", (2, 1), data)
self.assertEqual(image.size, (2, 1))
self.assertEqual(image.getpixel((0, 0)), (0, 0, 255))
self.assertEqual(image.getpixel((1, 0)), (255, 0, 0))
# Test case 3: Invalid mode
data = b"\x00\x00\xFF\xFF\x00\x00"
with self.assertRaises(ValueError):
Image.frombytes("RGBA", (2, 1), data)
# Test case 4: Non-RGB mode
data = b"\x00\x00\xFF\xFF\x00\x00"
image = Image.frombytes("L", (2, 1), data)
self.assertEqual(image.size, (2, 1))
self.assertEqual(image.getpixel((0, 0)), 0)
self.assertEqual(image.getpixel((1, 0)), 255)
# Test case 5: Zero width
data = b""
image = Image.frombytes("RGB", (0, 1), data)
self.assertEqual(image.size, (0, 1))
# Test case 6: Zero height
data = b""
image = Image.frombytes("RGB", (1, 0), data)
self.assertEqual(image.size, (1, 0))
# Test case 7: s[0] < 0
data = b"\x00\x00\xFF\xFF\x00\x00"
s = (-1, 1)
with self.assertRaises(ValueError):
Image.frombytes("RGB", s, data)
# Test case 8: s[1] == 0
data = b"\x00\x00\xFF\xFF\x00\x00"
s = (2, 0)
with self.assertRaises(ValueError):
Image.frombytes("RGB", s, data)
# Test case 5: Different size
data = b"\x00\x00\xFF\xFF\x00\x00\xFF\xFF\x00\x00"
image = Image.frombytes("RGB", (3, 1), data)
self.assertEqual(image.size, (3, 1))
self.assertEqual(image.getpixel((0, 0)), (0, 0, 255))
self.assertEqual(image.getpixel((1, 0)), (255, 0, 0))
self.assertEqual(image.getpixel((2, 0)), (255, 0, 0))
if __name__ == "__main__":
unittest.main()