From dae1f691c2cb0afc5c5d2a05c72a3e72d9ac84a0 Mon Sep 17 00:00:00 2001
From: Andrew Murray <radarhere@users.noreply.github.com>
Date: Mon, 21 Feb 2022 13:49:01 +1100
Subject: [PATCH] Updated checks that no warnings were raised

---
 Tests/test_bmp_reference.py |  9 +++------
 Tests/test_file_dcx.py      | 10 ++++------
 Tests/test_file_fli.py      | 10 ++++------
 Tests/test_file_gif.py      |  9 +++------
 Tests/test_file_icns.py     |  4 ++--
 Tests/test_file_im.py       |  9 +++------
 Tests/test_file_jpeg.py     |  4 ++--
 Tests/test_file_mpo.py      |  9 +++------
 Tests/test_file_png.py      |  4 ++--
 Tests/test_file_psd.py      | 10 ++++------
 Tests/test_file_spider.py   |  9 +++------
 Tests/test_file_tar.py      | 10 ++++------
 Tests/test_file_tiff.py     |  9 +++------
 Tests/test_file_webp.py     |  4 ++--
 Tests/test_image.py         |  4 ++--
 Tests/test_imageqt.py       |  6 +++---
 Tests/test_numpy.py         |  5 +++--
 17 files changed, 50 insertions(+), 75 deletions(-)

diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py
index 99e16391adc..440bc325b45 100644
--- a/Tests/test_bmp_reference.py
+++ b/Tests/test_bmp_reference.py
@@ -1,6 +1,5 @@
 import os
-
-import pytest
+import warnings
 
 from PIL import Image
 
@@ -20,16 +19,14 @@ def test_bad():
     either"""
     for f in get_files("b"):
 
-        with pytest.warns(None) as record:
+        # Assert that there is no unclosed file warning
+        with warnings.catch_warnings():
             try:
                 with Image.open(f) as im:
                     im.load()
             except Exception:  # as msg:
                 pass
 
-        # Assert that there is no unclosed file warning
-        assert not record
-
 
 def test_questionable():
     """These shouldn't crash/dos, but it's not well defined that these
diff --git a/Tests/test_file_dcx.py b/Tests/test_file_dcx.py
index 58d5cbf1a61..0f09c4b9915 100644
--- a/Tests/test_file_dcx.py
+++ b/Tests/test_file_dcx.py
@@ -1,3 +1,5 @@
+import warnings
+
 import pytest
 
 from PIL import DcxImagePlugin, Image
@@ -31,21 +33,17 @@ def open():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         im = Image.open(TEST_FILE)
         im.load()
         im.close()
 
-    assert not record
-
 
 def test_context_manager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with Image.open(TEST_FILE) as im:
             im.load()
 
-    assert not record
-
 
 def test_invalid_file():
     with open("Tests/images/flower.jpg", "rb") as fp:
diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py
index 675e06bf83c..c1ad4a7f0f9 100644
--- a/Tests/test_file_fli.py
+++ b/Tests/test_file_fli.py
@@ -1,3 +1,5 @@
+import warnings
+
 import pytest
 
 from PIL import FliImagePlugin, Image
@@ -38,21 +40,17 @@ def open():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         im = Image.open(static_test_file)
         im.load()
         im.close()
 
-    assert not record
-
 
 def test_context_manager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with Image.open(static_test_file) as im:
             im.load()
 
-    assert not record
-
 
 def test_tell():
     # Arrange
diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py
index 8b0306db892..64a551f8acc 100644
--- a/Tests/test_file_gif.py
+++ b/Tests/test_file_gif.py
@@ -1,3 +1,4 @@
+import warnings
 from io import BytesIO
 
 import pytest
@@ -39,21 +40,17 @@ def open():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         im = Image.open(TEST_GIF)
         im.load()
         im.close()
 
-    assert not record
-
 
 def test_context_manager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with Image.open(TEST_GIF) as im:
             im.load()
 
-    assert not record
-
 
 def test_invalid_file():
     invalid_file = "Tests/images/flower.jpg"
diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py
index a22fe040051..7d8f89184b1 100644
--- a/Tests/test_file_icns.py
+++ b/Tests/test_file_icns.py
@@ -1,5 +1,6 @@
 import io
 import os
+import warnings
 
 import pytest
 
@@ -19,9 +20,8 @@ def test_sanity():
     with Image.open(TEST_FILE) as im:
 
         # Assert that there is no unclosed file warning
-        with pytest.warns(None) as record:
+        with warnings.catch_warnings():
             im.load()
-        assert not record
 
         assert im.mode == "RGBA"
         assert im.size == (1024, 1024)
diff --git a/Tests/test_file_im.py b/Tests/test_file_im.py
index 9d25a4d1a8f..675210c30d2 100644
--- a/Tests/test_file_im.py
+++ b/Tests/test_file_im.py
@@ -1,4 +1,5 @@
 import filecmp
+import warnings
 
 import pytest
 
@@ -35,21 +36,17 @@ def open():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         im = Image.open(TEST_IM)
         im.load()
         im.close()
 
-    assert not record
-
 
 def test_context_manager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with Image.open(TEST_IM) as im:
             im.load()
 
-    assert not record
-
 
 def test_tell():
     # Arrange
diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py
index f0cfb7811c4..e5e8c85f4ed 100644
--- a/Tests/test_file_jpeg.py
+++ b/Tests/test_file_jpeg.py
@@ -1,5 +1,6 @@
 import os
 import re
+import warnings
 from io import BytesIO
 
 import pytest
@@ -756,9 +757,8 @@ def test_exif_x_resolution(self, tmp_path):
             assert exif[282] == 180
 
             out = str(tmp_path / "out.jpg")
-            with pytest.warns(None) as record:
+            with warnings.catch_warnings():
                 im.save(out, exif=exif)
-            assert not record
 
         with Image.open(out) as reloaded:
             assert reloaded.getexif()[282] == 180
diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py
index 9de09645856..0e59e4d56c3 100644
--- a/Tests/test_file_mpo.py
+++ b/Tests/test_file_mpo.py
@@ -1,3 +1,4 @@
+import warnings
 from io import BytesIO
 
 import pytest
@@ -41,21 +42,17 @@ def open():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         im = Image.open(test_files[0])
         im.load()
         im.close()
 
-    assert not record
-
 
 def test_context_manager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with Image.open(test_files[0]) as im:
             im.load()
 
-    assert not record
-
 
 def test_app():
     for test_file in test_files:
diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py
index 0869cc58bc5..bb2b0d11918 100644
--- a/Tests/test_file_png.py
+++ b/Tests/test_file_png.py
@@ -1,5 +1,6 @@
 import re
 import sys
+import warnings
 import zlib
 from io import BytesIO
 
@@ -331,9 +332,8 @@ def test_load_verify(self):
 
         with Image.open(TEST_PNG_FILE) as im:
             # Assert that there is no unclosed file warning
-            with pytest.warns(None) as record:
+            with warnings.catch_warnings():
                 im.verify()
-            assert not record
 
         with Image.open(TEST_PNG_FILE) as im:
             im.load()
diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py
index a7f379e5570..b4b5b7a0c65 100644
--- a/Tests/test_file_psd.py
+++ b/Tests/test_file_psd.py
@@ -1,3 +1,5 @@
+import warnings
+
 import pytest
 
 from PIL import Image, PsdImagePlugin
@@ -29,21 +31,17 @@ def open():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         im = Image.open(test_file)
         im.load()
         im.close()
 
-    assert not record
-
 
 def test_context_manager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with Image.open(test_file) as im:
             im.load()
 
-    assert not record
-
 
 def test_invalid_file():
     invalid_file = "Tests/images/flower.jpg"
diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py
index 3c93160f11d..0e3b705a295 100644
--- a/Tests/test_file_spider.py
+++ b/Tests/test_file_spider.py
@@ -1,4 +1,5 @@
 import tempfile
+import warnings
 from io import BytesIO
 
 import pytest
@@ -28,21 +29,17 @@ def open():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         im = Image.open(TEST_FILE)
         im.load()
         im.close()
 
-    assert not record
-
 
 def test_context_manager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with Image.open(TEST_FILE) as im:
             im.load()
 
-    assert not record
-
 
 def test_save(tmp_path):
     # Arrange
diff --git a/Tests/test_file_tar.py b/Tests/test_file_tar.py
index b38727fb9b2..5daab47fca3 100644
--- a/Tests/test_file_tar.py
+++ b/Tests/test_file_tar.py
@@ -1,3 +1,5 @@
+import warnings
+
 import pytest
 
 from PIL import Image, TarIO, features
@@ -31,16 +33,12 @@ def open():
 
 
 def test_close():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
         tar.close()
 
-    assert not record
-
 
 def test_contextmanager():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"):
             pass
-
-    assert not record
diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py
index fc4fc2a1b5e..28aeff075cb 100644
--- a/Tests/test_file_tiff.py
+++ b/Tests/test_file_tiff.py
@@ -1,4 +1,5 @@
 import os
+import warnings
 from io import BytesIO
 
 import pytest
@@ -64,20 +65,16 @@ def open():
         pytest.warns(ResourceWarning, open)
 
     def test_closed_file(self):
-        with pytest.warns(None) as record:
+        with warnings.catch_warnings():
             im = Image.open("Tests/images/multipage.tiff")
             im.load()
             im.close()
 
-        assert not record
-
     def test_context_manager(self):
-        with pytest.warns(None) as record:
+        with warnings.catch_warnings():
             with Image.open("Tests/images/multipage.tiff") as im:
                 im.load()
 
-        assert not record
-
     def test_mac_tiff(self):
         # Read RGBa images from macOS [@PIL136]
 
diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py
index e72b4993c63..55897f1eb5a 100644
--- a/Tests/test_file_webp.py
+++ b/Tests/test_file_webp.py
@@ -1,6 +1,7 @@
 import io
 import re
 import sys
+import warnings
 
 import pytest
 
@@ -161,9 +162,8 @@ def test_no_resource_warning(self, tmp_path):
         file_path = "Tests/images/hopper.webp"
         with Image.open(file_path) as image:
             temp_file = str(tmp_path / "temp.webp")
-            with pytest.warns(None) as record:
+            with warnings.catch_warnings():
                 image.save(temp_file)
-            assert not record
 
     def test_file_pointer_could_be_reused(self):
         file_path = "Tests/images/hopper.webp"
diff --git a/Tests/test_image.py b/Tests/test_image.py
index b616d06ff08..2cd858df16f 100644
--- a/Tests/test_image.py
+++ b/Tests/test_image.py
@@ -3,6 +3,7 @@
 import shutil
 import sys
 import tempfile
+import warnings
 
 import pytest
 
@@ -648,9 +649,8 @@ def test_no_resource_warning_on_save(self, tmp_path):
 
         # Act/Assert
         with Image.open(test_file) as im:
-            with pytest.warns(None) as record:
+            with warnings.catch_warnings():
                 im.save(temp_file)
-            assert not record
 
     def test_load_on_nonexclusive_multiframe(self):
         with open("Tests/images/frozenpond.mpo", "rb") as fp:
diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py
index 589cb5a210a..93090793970 100644
--- a/Tests/test_imageqt.py
+++ b/Tests/test_imageqt.py
@@ -1,3 +1,5 @@
+import warnings
+
 import pytest
 
 from PIL import ImageQt
@@ -56,7 +58,5 @@ def test_image():
 
 
 def test_closed_file():
-    with pytest.warns(None) as record:
+    with warnings.catch_warnings():
         ImageQt.ImageQt("Tests/images/hopper.gif")
-
-    assert not record
diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py
index 936474fe8d2..9735837bcba 100644
--- a/Tests/test_numpy.py
+++ b/Tests/test_numpy.py
@@ -1,3 +1,5 @@
+import warnings
+
 import pytest
 
 from PIL import Image
@@ -237,6 +239,5 @@ def test_no_resource_warning_for_numpy_array():
     with Image.open(test_file) as im:
 
         # Act/Assert
-        with pytest.warns(None) as record:
+        with warnings.catch_warnings():
             array(im)
-        assert not record
