Skip to content

Commit 38bf862

Browse files
committed
Replace PillowTestCase.assert_warning with pytest.warns
1 parent 63881ab commit 38bf862

24 files changed

Lines changed: 72 additions & 76 deletions

Tests/helper.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -178,31 +178,6 @@ def delete_tempfile(self, path):
178178
except OSError:
179179
pass # report?
180180

181-
def assert_warning(self, warn_class, func, *args, **kwargs):
182-
import warnings
183-
184-
with warnings.catch_warnings(record=True) as w:
185-
# Cause all warnings to always be triggered.
186-
warnings.simplefilter("always")
187-
188-
# Hopefully trigger a warning.
189-
result = func(*args, **kwargs)
190-
191-
# Verify some things.
192-
if warn_class is None:
193-
self.assertEqual(
194-
len(w), 0, "Expected no warnings, got %s" % [v.category for v in w]
195-
)
196-
else:
197-
self.assertGreaterEqual(len(w), 1)
198-
found = False
199-
for v in w:
200-
if issubclass(v.category, warn_class):
201-
found = True
202-
break
203-
self.assertTrue(found)
204-
return result
205-
206181
def tempfile(self, template):
207182
assert template[:5] in ("temp.", "temp_")
208183
fd, path = tempfile.mkstemp(template[4:], template[:4])

Tests/test_bmp_reference.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22

3+
import pytest
34
from PIL import Image
45

56
from .helper import PillowTestCase, assert_image_similar
@@ -28,7 +29,7 @@ def open(f):
2829
pass
2930

3031
# Assert that there is no unclosed file warning
31-
self.assert_warning(None, open, f)
32+
pytest.warns(None, open, f)
3233

3334
def test_questionable(self):
3435
""" These shouldn't crash/dos, but it's not well defined that these

Tests/test_core_resources.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import unittest
33

4+
import pytest
45
from PIL import Image
56

67
from .helper import PillowTestCase, is_pypy
@@ -169,12 +170,12 @@ def test_units(self):
169170
self.assertEqual(Image.core.get_block_size(), 2 * 1024 * 1024)
170171

171172
def test_warnings(self):
172-
self.assert_warning(
173+
pytest.warns(
173174
UserWarning, Image._apply_env_variables, {"PILLOW_ALIGNMENT": "15"}
174175
)
175-
self.assert_warning(
176+
pytest.warns(
176177
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCK_SIZE": "1024"}
177178
)
178-
self.assert_warning(
179+
pytest.warns(
179180
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCKS_MAX": "wat"}
180181
)

Tests/test_decompression_bomb.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from PIL import Image
23

34
from .helper import PillowTestCase, hopper
@@ -38,7 +39,7 @@ def open():
3839
with Image.open(TEST_FILE):
3940
pass
4041

41-
self.assert_warning(Image.DecompressionBombWarning, open)
42+
pytest.warns(Image.DecompressionBombWarning, open)
4243

4344
def test_exception(self):
4445
# Set limit to trigger exception on the test file
@@ -71,7 +72,7 @@ def testEnlargeCrop(self):
7172
# Crops can extend the extents, therefore we should have the
7273
# same decompression bomb warnings on them.
7374
box = (0, 0, self.src.width * 2, self.src.height * 2)
74-
self.assert_warning(Image.DecompressionBombWarning, self.src.crop, box)
75+
pytest.warns(Image.DecompressionBombWarning, self.src.crop, box)
7576

7677
def test_crop_decompression_checks(self):
7778

@@ -87,7 +88,7 @@ def test_crop_decompression_checks(self):
8788
self.assertEqual(im.crop(value).size, (9, 9))
8889

8990
for value in warning_values:
90-
self.assert_warning(Image.DecompressionBombWarning, im.crop, value)
91+
pytest.warns(Image.DecompressionBombWarning, im.crop, value)
9192

9293
for value in error_values:
9394
with self.assertRaises(Image.DecompressionBombError):

Tests/test_file_dcx.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
import pytest
34
from PIL import DcxImagePlugin, Image
45

56
from .helper import PillowTestCase, assert_image_equal, hopper, is_pypy
@@ -27,22 +28,22 @@ def open():
2728
im = Image.open(TEST_FILE)
2829
im.load()
2930

30-
self.assert_warning(ResourceWarning, open)
31+
pytest.warns(ResourceWarning, open)
3132

3233
def test_closed_file(self):
3334
def open():
3435
im = Image.open(TEST_FILE)
3536
im.load()
3637
im.close()
3738

38-
self.assert_warning(None, open)
39+
pytest.warns(None, open)
3940

4041
def test_context_manager(self):
4142
def open():
4243
with Image.open(TEST_FILE) as im:
4344
im.load()
4445

45-
self.assert_warning(None, open)
46+
pytest.warns(None, open)
4647

4748
def test_invalid_file(self):
4849
with open("Tests/images/flower.jpg", "rb") as fp:

Tests/test_file_fli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
import pytest
34
from PIL import FliImagePlugin, Image
45

56
from .helper import PillowTestCase, assert_image_equal, is_pypy
@@ -34,22 +35,22 @@ def open():
3435
im = Image.open(static_test_file)
3536
im.load()
3637

37-
self.assert_warning(ResourceWarning, open)
38+
pytest.warns(ResourceWarning, open)
3839

3940
def test_closed_file(self):
4041
def open():
4142
im = Image.open(static_test_file)
4243
im.load()
4344
im.close()
4445

45-
self.assert_warning(None, open)
46+
pytest.warns(None, open)
4647

4748
def test_context_manager(self):
4849
def open():
4950
with Image.open(static_test_file) as im:
5051
im.load()
5152

52-
self.assert_warning(None, open)
53+
pytest.warns(None, open)
5354

5455
def test_tell(self):
5556
# Arrange

Tests/test_file_gif.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
from io import BytesIO
33

4+
import pytest
45
from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette
56

67
from .helper import (
@@ -47,22 +48,22 @@ def open():
4748
im = Image.open(TEST_GIF)
4849
im.load()
4950

50-
self.assert_warning(ResourceWarning, open)
51+
pytest.warns(ResourceWarning, open)
5152

5253
def test_closed_file(self):
5354
def open():
5455
im = Image.open(TEST_GIF)
5556
im.load()
5657
im.close()
5758

58-
self.assert_warning(None, open)
59+
pytest.warns(None, open)
5960

6061
def test_context_manager(self):
6162
def open():
6263
with Image.open(TEST_GIF) as im:
6364
im.load()
6465

65-
self.assert_warning(None, open)
66+
pytest.warns(None, open)
6667

6768
def test_invalid_file(self):
6869
invalid_file = "Tests/images/flower.jpg"
@@ -684,7 +685,7 @@ def test_rgb_transparency(self):
684685
# Single frame
685686
im = Image.new("RGB", (1, 1))
686687
im.info["transparency"] = (255, 0, 0)
687-
self.assert_warning(UserWarning, im.save, out)
688+
pytest.warns(UserWarning, im.save, out)
688689

689690
with Image.open(out) as reloaded:
690691
self.assertNotIn("transparency", reloaded.info)
@@ -693,7 +694,7 @@ def test_rgb_transparency(self):
693694
im = Image.new("RGB", (1, 1))
694695
im.info["transparency"] = b""
695696
ims = [Image.new("RGB", (1, 1))]
696-
self.assert_warning(UserWarning, im.save, out, save_all=True, append_images=ims)
697+
pytest.warns(UserWarning, im.save, out, save_all=True, append_images=ims)
697698

698699
with Image.open(out) as reloaded:
699700
self.assertNotIn("transparency", reloaded.info)

Tests/test_file_icns.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import unittest
44

5+
import pytest
56
from PIL import IcnsImagePlugin, Image
67

78
from .helper import PillowTestCase, assert_image_equal, assert_image_similar
@@ -19,7 +20,7 @@ def test_sanity(self):
1920
with Image.open(TEST_FILE) as im:
2021

2122
# Assert that there is no unclosed file warning
22-
self.assert_warning(None, im.load)
23+
pytest.warns(None, im.load)
2324

2425
self.assertEqual(im.mode, "RGBA")
2526
self.assertEqual(im.size, (1024, 1024))

Tests/test_file_ico.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io
22

3+
import pytest
34
from PIL import IcoImagePlugin, Image, ImageDraw
45

56
from .helper import PillowTestCase, assert_image_equal, hopper
@@ -87,7 +88,7 @@ def open():
8788
with Image.open("Tests/images/hopper_unexpected.ico") as im:
8889
self.assertEqual(im.size, (16, 16))
8990

90-
self.assert_warning(UserWarning, open)
91+
pytest.warns(UserWarning, open)
9192

9293
def test_draw_reloaded(self):
9394
with Image.open(TEST_ICO_FILE) as im:

Tests/test_file_im.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
import pytest
34
from PIL import Image, ImImagePlugin
45

56
from .helper import PillowTestCase, assert_image_equal, hopper, is_pypy
@@ -22,22 +23,22 @@ def open():
2223
im = Image.open(TEST_IM)
2324
im.load()
2425

25-
self.assert_warning(ResourceWarning, open)
26+
pytest.warns(ResourceWarning, open)
2627

2728
def test_closed_file(self):
2829
def open():
2930
im = Image.open(TEST_IM)
3031
im.load()
3132
im.close()
3233

33-
self.assert_warning(None, open)
34+
pytest.warns(None, open)
3435

3536
def test_context_manager(self):
3637
def open():
3738
with Image.open(TEST_IM) as im:
3839
im.load()
3940

40-
self.assert_warning(None, open)
41+
pytest.warns(None, open)
4142

4243
def test_tell(self):
4344
# Arrange

0 commit comments

Comments
 (0)