From 6b50ba07fd4f2bf6ed13c9b11e743072658c5863 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 5 Aug 2017 21:58:31 +0300 Subject: [PATCH] add tests for Image.new modes --- Tests/test_image.py | 19 +++++++++++++++++++ libImaging/Storage.c | 7 ++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 1f9c4d798..a83b917a0 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -7,6 +7,25 @@ import sys class TestImage(PillowTestCase): + def test_image_modes_success(self): + for mode in [ + '1', 'P', 'PA', + 'L', 'LA', 'La', + 'F', 'I', 'I;16', 'I;16L', 'I;16B', 'I;16N', + 'RGB', 'RGBX', 'RGBA', 'RGBa', + 'CMYK', 'YCbCr', 'LAB', 'HSV', + ]: + Image.new(mode, (1, 1)) + + def test_image_modes_fail(self): + for mode in [ + '', 'bad', 'very very long', + 'BGR;15', 'BGR;16', 'BGR;24', 'BGR;32' + ]: + with self.assertRaises(ValueError) as e: + Image.new(mode, (1, 1)); + self.assertEqual(e.exception.message, 'unrecognized image mode') + def test_sanity(self): im = Image.new("L", (100, 100)) diff --git a/libImaging/Storage.c b/libImaging/Storage.c index 5cddfd66c..6acda2af0 100644 --- a/libImaging/Storage.c +++ b/libImaging/Storage.c @@ -205,7 +205,7 @@ ImagingNewPrologueSubtype(const char *mode, int xsize, int ysize, int size) } else { free(im); - return (Imaging) ImagingError_ValueError("unrecognized mode"); + return (Imaging) ImagingError_ValueError("unrecognized image mode"); } /* Setup image descriptor */ @@ -387,16 +387,13 @@ ImagingNew(const char* mode, int xsize, int ysize) int bytes; Imaging im; - if (strcmp(mode, "") == 0) - return (Imaging) ImagingError_ValueError("empty mode"); - if (strlen(mode) == 1) { if (mode[0] == 'F' || mode[0] == 'I') bytes = 4; else bytes = 1; } else - bytes = strlen(mode); /* close enough */ + bytes = strlen(mode) || 1; /* close enough */ if (xsize < 0 || ysize < 0) { return (Imaging) ImagingError_ValueError("bad image size");