From dda6145fce8fbbe82db4d92cde41a64136ec02df Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 7 Apr 2020 09:58:21 +0300 Subject: [PATCH] Since Python 3.3 IOError and WindowsError have been merged into OSError --- Tests/check_j2k_overflow.py | 2 +- Tests/check_libtiff_segfault.py | 2 +- Tests/test_file_bufrstub.py | 4 ++-- Tests/test_file_dds.py | 4 ++-- Tests/test_file_fitsstub.py | 6 +++--- Tests/test_file_fpx.py | 2 +- Tests/test_file_gribstub.py | 4 ++-- Tests/test_file_hdf5stub.py | 6 +++--- Tests/test_file_jpeg.py | 12 ++++++------ Tests/test_file_jpeg2k.py | 2 +- Tests/test_file_libtiff.py | 8 ++++---- Tests/test_file_msp.py | 2 +- Tests/test_file_palm.py | 8 ++++---- Tests/test_file_pdf.py | 2 +- Tests/test_file_png.py | 12 ++++++------ Tests/test_file_ppm.py | 2 +- Tests/test_file_psd.py | 2 +- Tests/test_file_spider.py | 2 +- Tests/test_file_tiff.py | 6 +++--- Tests/test_file_webp.py | 2 +- Tests/test_file_wmf.py | 2 +- Tests/test_image.py | 4 ++-- Tests/test_imagefile.py | 16 ++++++++-------- Tests/test_imagefont.py | 14 +++++++------- Tests/test_imagegrab.py | 8 ++++---- Tests/test_imagepalette.py | 2 +- Tests/test_map.py | 2 +- Tests/test_sgi_crash.py | 2 +- docs/handbook/image-file-formats.rst | 2 +- docs/handbook/tutorial.rst | 10 +++++----- docs/releasenotes/7.0.0.rst | 2 +- selftest.py | 2 +- setup.py | 2 +- src/PIL/FpxImagePlugin.py | 2 +- src/PIL/GdImageFile.py | 2 +- src/PIL/GifImagePlugin.py | 2 +- src/PIL/Image.py | 2 +- src/PIL/ImageFile.py | 20 ++++++++++---------- src/PIL/ImageFont.py | 14 +++++++------- src/PIL/ImageGrab.py | 2 +- src/PIL/TiffImagePlugin.py | 4 ++-- src/PIL/__init__.py | 2 +- src/_imaging.c | 4 ++-- src/_imagingcms.c | 12 ++++++------ src/_imagingft.c | 6 +++--- src/_webp.c | 4 ++-- src/display.c | 22 +++++++++++----------- src/encode.c | 2 +- src/libImaging/Except.c | 2 +- src/libImaging/File.c | 2 +- src/libImaging/Imaging.h | 2 +- src/map.c | 6 +++--- 52 files changed, 135 insertions(+), 135 deletions(-) diff --git a/Tests/check_j2k_overflow.py b/Tests/check_j2k_overflow.py index f20ad6748..7a0a5f948 100644 --- a/Tests/check_j2k_overflow.py +++ b/Tests/check_j2k_overflow.py @@ -5,5 +5,5 @@ from PIL import Image def test_j2k_overflow(tmp_path): im = Image.new("RGBA", (1024, 131584)) target = str(tmp_path / "temp.jpc") - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(target) diff --git a/Tests/check_libtiff_segfault.py b/Tests/check_libtiff_segfault.py index 5187385d6..6663ac097 100644 --- a/Tests/check_libtiff_segfault.py +++ b/Tests/check_libtiff_segfault.py @@ -9,6 +9,6 @@ def test_libtiff_segfault(): libtiff >= 4.0.0 """ - with pytest.raises(IOError): + with pytest.raises(OSError): with Image.open(TEST_FILE) as im: im.load() diff --git a/Tests/test_file_bufrstub.py b/Tests/test_file_bufrstub.py index ee6f3f2a4..6803a1230 100644 --- a/Tests/test_file_bufrstub.py +++ b/Tests/test_file_bufrstub.py @@ -32,7 +32,7 @@ def test_load(): with Image.open(TEST_FILE) as im: # Act / Assert: stub cannot load without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() @@ -42,5 +42,5 @@ def test_save(tmp_path): tmpfile = str(tmp_path / "temp.bufr") # Act / Assert: stub cannot save without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(tmpfile) diff --git a/Tests/test_file_dds.py b/Tests/test_file_dds.py index d157e15fd..8960edea3 100644 --- a/Tests/test_file_dds.py +++ b/Tests/test_file_dds.py @@ -138,7 +138,7 @@ def test_short_header(): def short_header(): Image.open(BytesIO(img_file[:119])) - with pytest.raises(IOError): + with pytest.raises(OSError): short_header() @@ -152,7 +152,7 @@ def test_short_file(): with Image.open(BytesIO(img_file[:-100])) as im: im.load() - with pytest.raises(IOError): + with pytest.raises(OSError): short_file() diff --git a/Tests/test_file_fitsstub.py b/Tests/test_file_fitsstub.py index f9f6c4ba9..01bc2deee 100644 --- a/Tests/test_file_fitsstub.py +++ b/Tests/test_file_fitsstub.py @@ -30,7 +30,7 @@ def test_load(): with Image.open(TEST_FILE) as im: # Act / Assert: stub cannot load without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() @@ -41,7 +41,7 @@ def test_save(): dummy_filename = "dummy.filename" # Act / Assert: stub cannot save without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(dummy_filename) - with pytest.raises(IOError): + with pytest.raises(OSError): FitsStubImagePlugin._save(im, dummy_fp, dummy_filename) diff --git a/Tests/test_file_fpx.py b/Tests/test_file_fpx.py index ef8cdb577..a247de79c 100644 --- a/Tests/test_file_fpx.py +++ b/Tests/test_file_fpx.py @@ -19,5 +19,5 @@ def test_invalid_file(): def test_fpx_invalid_number_of_bands(): - with pytest.raises(IOError, match="Invalid number of bands"): + with pytest.raises(OSError, match="Invalid number of bands"): Image.open("Tests/images/input_bw_five_bands.fpx") diff --git a/Tests/test_file_gribstub.py b/Tests/test_file_gribstub.py index 1cc1f47ac..9d9def96b 100644 --- a/Tests/test_file_gribstub.py +++ b/Tests/test_file_gribstub.py @@ -32,7 +32,7 @@ def test_load(): with Image.open(TEST_FILE) as im: # Act / Assert: stub cannot load without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() @@ -42,5 +42,5 @@ def test_save(tmp_path): tmpfile = str(tmp_path / "temp.grib") # Act / Assert: stub cannot save without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(tmpfile) diff --git a/Tests/test_file_hdf5stub.py b/Tests/test_file_hdf5stub.py index 526fd7c99..862cafa91 100644 --- a/Tests/test_file_hdf5stub.py +++ b/Tests/test_file_hdf5stub.py @@ -30,7 +30,7 @@ def test_load(): with Image.open(TEST_FILE) as im: # Act / Assert: stub cannot load without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() @@ -41,7 +41,7 @@ def test_save(): dummy_filename = "dummy.filename" # Act / Assert: stub cannot save without an implemented handler - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(dummy_filename) - with pytest.raises(IOError): + with pytest.raises(OSError): Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 330451228..9f963d80f 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -147,7 +147,7 @@ class TestFileJpeg: with Image.open("Tests/images/icc_profile_big.jpg") as im: f = str(tmp_path / "temp.jpg") icc_profile = im.info["icc_profile"] - # Should not raise IOError for image with icc larger than image size. + # Should not raise OSError for image with icc larger than image size. im.save( f, format="JPEG", @@ -379,14 +379,14 @@ class TestFileJpeg: ImageFile.LOAD_TRUNCATED_IMAGES = False assert im.getbbox() is not None - def test_truncated_jpeg_throws_IOError(self): + def test_truncated_jpeg_throws_oserror(self): filename = "Tests/images/truncated_jpeg.jpg" with Image.open(filename) as im: - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() # Test that the error is raised if loaded a second time - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() def test_qtables(self, tmp_path): @@ -552,7 +552,7 @@ class TestFileJpeg: out = BytesIO() for mode in ["LA", "La", "RGBA", "RGBa", "P"]: img = Image.new(mode, (20, 20)) - with pytest.raises(IOError): + with pytest.raises(OSError): img.save(out, "JPEG") def test_save_tiff_with_dpi(self, tmp_path): @@ -702,7 +702,7 @@ class TestFileCloseW32: im = Image.open(tmpfile) fp = im.fp assert not fp.closed - with pytest.raises(WindowsError): + with pytest.raises(OSError): os.remove(tmpfile) im.load() assert fp.closed diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 72bc7df67..7b8b7a04a 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -218,7 +218,7 @@ def test_16bit_jp2_roundtrips(): def test_unbound_local(): # prepatch, a malformed jp2 file could cause an UnboundLocalError exception. - with pytest.raises(IOError): + with pytest.raises(OSError): Image.open("Tests/images/unbound_variable.jp2") diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 923bd6107..9523e5901 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -479,11 +479,11 @@ class TestFileLibTiff(LibTiffTestCase): im = hopper("RGB") out = str(tmp_path / "temp.tif") - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(out, compression="tiff_ccitt") - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(out, compression="group3") - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(out, compression="group4") def test_fp_leak(self): @@ -831,7 +831,7 @@ class TestFileLibTiff(LibTiffTestCase): def test_realloc_overflow(self): TiffImagePlugin.READ_LIBTIFF = True with Image.open("Tests/images/tiff_overflow_rows_per_strip.tif") as im: - with pytest.raises(IOError) as e: + with pytest.raises(OSError) as e: im.load() # Assert that the error code is IMAGING_CODEC_MEMORY diff --git a/Tests/test_file_msp.py b/Tests/test_file_msp.py index 8f261388e..9b508a4e4 100644 --- a/Tests/test_file_msp.py +++ b/Tests/test_file_msp.py @@ -86,5 +86,5 @@ def test_cannot_save_wrong_mode(tmp_path): filename = str(tmp_path / "temp.msp") # Act/Assert - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(filename) diff --git a/Tests/test_file_palm.py b/Tests/test_file_palm.py index 886332dea..38f6dccd9 100644 --- a/Tests/test_file_palm.py +++ b/Tests/test_file_palm.py @@ -72,19 +72,19 @@ def test_p_mode(tmp_path): roundtrip(tmp_path, mode) -def test_l_ioerror(tmp_path): +def test_l_oserror(tmp_path): # Arrange mode = "L" # Act / Assert - with pytest.raises(IOError): + with pytest.raises(OSError): helper_save_as_palm(tmp_path, mode) -def test_rgb_ioerror(tmp_path): +def test_rgb_oserror(tmp_path): # Arrange mode = "RGB" # Act / Assert - with pytest.raises(IOError): + with pytest.raises(OSError): helper_save_as_palm(tmp_path, mode) diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index ea3b6c1d9..14a7a654f 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -172,7 +172,7 @@ def test_pdf_open(tmp_path): def test_pdf_append_fails_on_nonexistent_file(): im = hopper("RGB") with tempfile.TemporaryDirectory() as temp_dir: - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(os.path.join(temp_dir, "nonexistent.pdf"), append=True) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 476995dd7..a2535b97f 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -105,7 +105,7 @@ class TestFilePng: # file was checked into Subversion as a text file. test_file = "Tests/images/broken.png" - with pytest.raises(IOError): + with pytest.raises(OSError): Image.open(test_file) def test_bad_text(self): @@ -334,7 +334,7 @@ class TestFilePng: def test_verify_struct_error(self): # Check open/load/verify exception (#1755) - # offsets to test, -10: breaks in i32() in read. (IOError) + # offsets to test, -10: breaks in i32() in read. (OSError) # -13: breaks in crc, txt chunk. # -14: malformed chunk @@ -344,7 +344,7 @@ class TestFilePng: with Image.open(BytesIO(test_file)) as im: assert im.fp is not None - with pytest.raises((IOError, SyntaxError)): + with pytest.raises((OSError, SyntaxError)): im.verify() def test_verify_ignores_crc_error(self): @@ -463,7 +463,7 @@ class TestFilePng: data = b"\x89" + fd.read() pngfile = BytesIO(data) - with pytest.raises(IOError): + with pytest.raises(OSError): Image.open(pngfile) def test_trns_rgb(self): @@ -575,13 +575,13 @@ class TestFilePng: # Raises a SyntaxError in load_end with Image.open("Tests/images/broken_data_stream.png") as im: - with pytest.raises(IOError): + with pytest.raises(OSError): assert isinstance(im.text, dict) # Raises a UnicodeDecodeError in load_end with Image.open("Tests/images/truncated_image.png") as im: # The file is truncated - with pytest.raises(IOError): + with pytest.raises(OSError): im.text() ImageFile.LOAD_TRUNCATED_IMAGES = True assert isinstance(im.text, dict) diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 6b91ba28a..15c08e438 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -64,7 +64,7 @@ def test_neg_ppm(): # has been removed. The default opener doesn't accept negative # sizes. - with pytest.raises(IOError): + with pytest.raises(OSError): Image.open("Tests/images/negative_size.ppm") diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 4d8c6eba4..011efc977 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -125,5 +125,5 @@ def test_combined_larger_than_size(): # If we instead take the 'size' of the extra data field as the source of truth, # then the seek can't be negative - with pytest.raises(IOError): + with pytest.raises(OSError): Image.open("Tests/images/combined_larger_than_size.psd") diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index c7446e161..8c69491e6 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -134,7 +134,7 @@ def test_is_int_not_a_number(): def test_invalid_file(): invalid_file = "Tests/images/invalid.spider" - with pytest.raises(IOError): + with pytest.raises(OSError): Image.open(invalid_file) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index a996f0b0e..7aa55dad0 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -196,7 +196,7 @@ class TestFileTiff: def test_save_unsupported_mode(self, tmp_path): im = hopper("HSV") outfile = str(tmp_path / "temp.tif") - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(outfile) def test_little_endian(self): @@ -249,7 +249,7 @@ class TestFileTiff: assert im.getextrema() == (-3.140936851501465, 3.140684127807617) def test_unknown_pixel_mode(self): - with pytest.raises(IOError): + with pytest.raises(OSError): Image.open("Tests/images/hopper_unknown_pixel_mode.tif") def test_n_frames(self): @@ -614,7 +614,7 @@ class TestFileTiffW32: im = Image.open(tmpfile) fp = im.fp assert not fp.closed - with pytest.raises(WindowsError): + with pytest.raises(OSError): os.remove(tmpfile) im.load() assert fp.closed diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 22957f06d..1b8aa9f8a 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -22,7 +22,7 @@ class TestUnsupportedWebp: WebPImagePlugin.SUPPORTED = False file_path = "Tests/images/hopper.webp" - pytest.warns(UserWarning, lambda: pytest.raises(IOError, Image.open, file_path)) + pytest.warns(UserWarning, lambda: pytest.raises(OSError, Image.open, file_path)) if HAVE_WEBP: WebPImagePlugin.SUPPORTED = True diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 03444eb9d..9db4f6358 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -74,5 +74,5 @@ def test_save(tmp_path): for ext in [".wmf", ".emf"]: tmpfile = str(tmp_path / ("temp" + ext)) - with pytest.raises(IOError): + with pytest.raises(OSError): im.save(tmpfile) diff --git a/Tests/test_image.py b/Tests/test_image.py index 3a0b7bd62..4d1b66dff 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -57,7 +57,7 @@ class TestImage: assert str(e.value) == "unrecognized image mode" def test_exception_inheritance(self): - assert issubclass(UnidentifiedImageError, IOError) + assert issubclass(UnidentifiedImageError, OSError) def test_sanity(self): @@ -687,5 +687,5 @@ class TestRegistry: assert enc.args == ("RGB", "args", "extra") def test_encode_registry_fail(self): - with pytest.raises(IOError): + with pytest.raises(OSError): Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",)) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 883e3f566..e2b52857d 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -71,7 +71,7 @@ class TestImageFile: im1, im2 = roundtrip("JPEG") # lossy compression assert_image(im1, im2.mode, im2.size) - with pytest.raises(IOError): + with pytest.raises(OSError): roundtrip("PDF") def test_ico(self): @@ -93,9 +93,9 @@ class TestImageFile: assert_image_equal(im1, im2) - def test_raise_ioerror(self): - with pytest.raises(IOError): - ImageFile.raise_ioerror(1) + def test_raise_oserror(self): + with pytest.raises(OSError): + ImageFile.raise_oserror(1) def test_raise_typeerror(self): with pytest.raises(TypeError): @@ -107,17 +107,17 @@ class TestImageFile: input = f.read() p = ImageFile.Parser() p.feed(input) - with pytest.raises(IOError): + with pytest.raises(OSError): p.close() @skip_unless_feature("zlib") def test_truncated_with_errors(self): with Image.open("Tests/images/truncated_image.png") as im: - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() # Test that the error is raised if loaded a second time - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() @skip_unless_feature("zlib") @@ -132,7 +132,7 @@ class TestImageFile: @skip_unless_feature("zlib") def test_broken_datastream_with_errors(self): with Image.open("Tests/images/broken_data_stream.png") as im: - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() @skip_unless_feature("zlib") diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index e93aff4b2..0e642cde2 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -393,14 +393,14 @@ class TestImageFont: filename = "somefilenamethatdoesntexist.ttf" # Act/Assert - with pytest.raises(IOError): + with pytest.raises(OSError): ImageFont.load_path(filename) - with pytest.raises(IOError): + with pytest.raises(OSError): ImageFont.truetype(filename) def test_load_non_font_bytes(self): with open("Tests/images/hopper.jpg", "rb") as f: - with pytest.raises(IOError): + with pytest.raises(OSError): ImageFont.truetype(f) def test_default_font(self): @@ -615,9 +615,9 @@ class TestImageFont: font.get_variation_axes() return - with pytest.raises(IOError): + with pytest.raises(OSError): font.get_variation_names() - with pytest.raises(IOError): + with pytest.raises(OSError): font.get_variation_axes() font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf") @@ -669,7 +669,7 @@ class TestImageFont: font.set_variation_by_name("Bold") return - with pytest.raises(IOError): + with pytest.raises(OSError): font.set_variation_by_name("Bold") def _check_text(font, path, epsilon): @@ -701,7 +701,7 @@ class TestImageFont: font.set_variation_by_axes([100]) return - with pytest.raises(IOError): + with pytest.raises(OSError): font.set_variation_by_axes([500, 50]) def _check_text(font, path, epsilon): diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 790847734..82e746fda 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -31,23 +31,23 @@ class TestImageGrab: im2 = ImageGrab.grab(xdisplay="") assert_image(im2, im2.mode, im2.size) - except IOError as e: + except OSError as e: pytest.skip(str(e)) @pytest.mark.skipif(Image.core.HAVE_XCB, reason="tests missing XCB") def test_grab_no_xcb(self): if sys.platform not in ("win32", "darwin"): - with pytest.raises(IOError) as e: + with pytest.raises(OSError) as e: ImageGrab.grab() assert str(e.value).startswith("Pillow was built without XCB support") - with pytest.raises(IOError) as e: + with pytest.raises(OSError) as e: ImageGrab.grab(xdisplay="") assert str(e.value).startswith("Pillow was built without XCB support") @pytest.mark.skipif(not Image.core.HAVE_XCB, reason="requires XCB") def test_grab_invalid_xdisplay(self): - with pytest.raises(IOError) as e: + with pytest.raises(OSError) as e: ImageGrab.grab(xdisplay="error.test:0.0") assert str(e.value).startswith("X connection failed") diff --git a/Tests/test_imagepalette.py b/Tests/test_imagepalette.py index 29771cf03..4ef2d3ffd 100644 --- a/Tests/test_imagepalette.py +++ b/Tests/test_imagepalette.py @@ -145,5 +145,5 @@ def test_2bit_palette(tmp_path): def test_invalid_palette(): - with pytest.raises(IOError): + with pytest.raises(OSError): ImagePalette.load("Tests/images/hopper.jpg") diff --git a/Tests/test_map.py b/Tests/test_map.py index b2f3ff227..bdb59bfe0 100644 --- a/Tests/test_map.py +++ b/Tests/test_map.py @@ -20,7 +20,7 @@ def test_overflow(): # This image hits the offset test. with Image.open("Tests/images/l2rgb_read.bmp") as im: - with pytest.raises((ValueError, MemoryError, IOError)): + with pytest.raises((ValueError, MemoryError, OSError)): im.load() Image.MAX_IMAGE_PIXELS = max_pixels diff --git a/Tests/test_sgi_crash.py b/Tests/test_sgi_crash.py index 6f3fc6f5d..b1a3e1515 100644 --- a/Tests/test_sgi_crash.py +++ b/Tests/test_sgi_crash.py @@ -10,5 +10,5 @@ from PIL import Image def test_crashes(test_file): with open(test_file, "rb") as f: im = Image.open(f) - with pytest.raises(IOError): + with pytest.raises(OSError): im.load() diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index a12de82e2..40db9fe2b 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -1226,7 +1226,7 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum **append** Set to True to append pages to an existing PDF file. If the file doesn't - exist, an :py:exc:`IOError` will be raised. + exist, an :py:exc:`OSError` will be raised. .. versionadded:: 5.1.0 diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index 868402615..94a8e3aa1 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -29,7 +29,7 @@ bands in the image, and also the pixel type and depth. Common modes are “L” (luminance) for greyscale images, “RGB” for true color images, and “CMYK” for pre-press images. -If the file cannot be opened, an :py:exc:`IOError` exception is raised. +If the file cannot be opened, an :py:exc:`OSError` exception is raised. Once you have an instance of the :py:class:`~PIL.Image.Image` class, you can use the methods defined by this class to process and manipulate the image. For @@ -76,7 +76,7 @@ Convert files to JPEG try: with Image.open(infile) as im: im.save(outfile) - except IOError: + except OSError: print("cannot convert", infile) A second argument can be supplied to the :py:meth:`~PIL.Image.Image.save` @@ -100,7 +100,7 @@ Create JPEG thumbnails with Image.open(infile) as im: im.thumbnail(size) im.save(outfile, "JPEG") - except IOError: + except OSError: print("cannot create thumbnail for", infile) It is important to note that the library doesn’t decode or load the raster data @@ -125,7 +125,7 @@ Identify Image Files try: with Image.open(infile) as im: print(infile, im.format, "%dx%d" % im.size, im.mode) - except IOError: + except OSError: pass Cutting, pasting, and merging images @@ -450,7 +450,7 @@ context manager:: ... If everything goes well, the result is an :py:class:`PIL.Image.Image` object. -Otherwise, an :exc:`IOError` exception is raised. +Otherwise, an :exc:`OSError` exception is raised. You can use a file-like object instead of the filename. The object must implement :py:meth:`~file.read`, :py:meth:`~file.seek` and diff --git a/docs/releasenotes/7.0.0.rst b/docs/releasenotes/7.0.0.rst index e0e764342..73c44275c 100644 --- a/docs/releasenotes/7.0.0.rst +++ b/docs/releasenotes/7.0.0.rst @@ -85,7 +85,7 @@ Custom unidentified image error ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Pillow will now throw a custom ``UnidentifiedImageError`` when an image cannot be -identified. For backwards compatibility, this will inherit from ``IOError``. +identified. For backwards compatibility, this will inherit from ``OSError``. New argument ``reducing_gap`` for Image.resize() and Image.thumbnail() methods ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/selftest.py b/selftest.py index ea52256f7..a9a02ef71 100755 --- a/selftest.py +++ b/selftest.py @@ -47,7 +47,7 @@ def testimage(): ('PPM', 'RGB', (128, 128)) >>> try: ... _info(Image.open("Tests/images/hopper.jpg")) - ... except IOError as v: + ... except OSError as v: ... print(v) ('JPEG', 'RGB', (128, 128)) diff --git a/setup.py b/setup.py index 3e1a812b6..1236b5df5 100755 --- a/setup.py +++ b/setup.py @@ -579,7 +579,7 @@ class pil_build_ext(build_ext): try: listdir = os.listdir(directory) except Exception: - # WindowsError, FileNotFoundError + # OSError, FileNotFoundError continue for name in listdir: if name.startswith("openjpeg-") and os.path.isfile( diff --git a/src/PIL/FpxImagePlugin.py b/src/PIL/FpxImagePlugin.py index 8d252c79c..81501e244 100644 --- a/src/PIL/FpxImagePlugin.py +++ b/src/PIL/FpxImagePlugin.py @@ -99,7 +99,7 @@ class FpxImageFile(ImageFile.ImageFile): colors = [] bands = i32(s, 4) if bands > 4: - raise IOError("Invalid number of bands") + raise OSError("Invalid number of bands") for i in range(bands): # note: for now, we ignore the "uncalibrated" flag colors.append(i32(s, 8 + i * 4) & 0x7FFFFFFF) diff --git a/src/PIL/GdImageFile.py b/src/PIL/GdImageFile.py index 54c88712c..b3ab01a4e 100644 --- a/src/PIL/GdImageFile.py +++ b/src/PIL/GdImageFile.py @@ -74,7 +74,7 @@ def open(fp, mode="r"): :param mode: Optional mode. In this version, if the mode argument is given, it must be "r". :returns: An image instance. - :raises IOError: If the image could not be read. + :raises OSError: If the image could not be read. """ if mode != "r": raise ValueError("bad mode") diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 1d94fc7c7..9d360beae 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -255,7 +255,7 @@ class GifImageFile(ImageFile.ImageFile): else: pass - # raise IOError, "illegal GIF tag `%x`" % i8(s) + # raise OSError, "illegal GIF tag `%x`" % i8(s) try: if self.disposal_method < 2: diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 3ced965e8..19ddcbba5 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2080,7 +2080,7 @@ class Image: :returns: None :exception ValueError: If the output format could not be determined from the file name. Use the format option to solve this. - :exception IOError: If the file could not be written. The file + :exception OSError: If the file could not be written. The file may have been created, and may contain partial data. """ diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 628796865..2177dbf52 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -49,7 +49,12 @@ ERRORS = { } -def raise_ioerror(error): +# +# -------------------------------------------------------------------- +# Helpers + + +def raise_oserror(error): try: message = Image.core.getcodecstatus(error) except AttributeError: @@ -59,11 +64,6 @@ def raise_ioerror(error): raise OSError(message + " when reading image file") -# -# -------------------------------------------------------------------- -# Helpers - - def _tilesort(t): # sort on offset return t[2] @@ -267,7 +267,7 @@ class ImageFile(Image.Image): if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0: # still raised if decoder fails to return anything - raise_ioerror(err_code) + raise_oserror(err_code) return Image.Image.load(self) @@ -358,7 +358,7 @@ class Parser: (Consumer) Feed data to the parser. :param data: A string buffer. - :exception IOError: If the parser failed to parse the image file. + :exception OSError: If the parser failed to parse the image file. """ # collect data @@ -390,7 +390,7 @@ class Parser: if e < 0: # decoding error self.image = None - raise_ioerror(e) + raise_oserror(e) else: # end of image return @@ -444,7 +444,7 @@ class Parser: (Consumer) Close the stream. :returns: An image object. - :exception IOError: If the parser failed to parse the image file either + :exception OSError: If the parser failed to parse the image file either because it cannot be identified or cannot be decoded. """ diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 027e4c42e..25ceaa16a 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -499,7 +499,7 @@ class FreeTypeFont: def get_variation_names(self): """ :returns: A list of the named styles in a variation font. - :exception IOError: If the font is not a variation font. + :exception OSError: If the font is not a variation font. """ try: names = self.font.getvarnames() @@ -510,7 +510,7 @@ class FreeTypeFont: def set_variation_by_name(self, name): """ :param name: The name of the style. - :exception IOError: If the font is not a variation font. + :exception OSError: If the font is not a variation font. """ names = self.get_variation_names() if not isinstance(name, bytes): @@ -529,7 +529,7 @@ class FreeTypeFont: def get_variation_axes(self): """ :returns: A list of the axes in a variation font. - :exception IOError: If the font is not a variation font. + :exception OSError: If the font is not a variation font. """ try: axes = self.font.getvaraxes() @@ -542,7 +542,7 @@ class FreeTypeFont: def set_variation_by_axes(self, axes): """ :param axes: A list of values for each axis. - :exception IOError: If the font is not a variation font. + :exception OSError: If the font is not a variation font. """ try: self.font.setvaraxes(axes) @@ -586,7 +586,7 @@ def load(filename): :param filename: Name of font file. :return: A font object. - :exception IOError: If the file could not be read. + :exception OSError: If the file could not be read. """ f = ImageFont() f._load_pilfont(filename) @@ -638,7 +638,7 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None): :param layout_engine: Which layout engine to use, if available: `ImageFont.LAYOUT_BASIC` or `ImageFont.LAYOUT_RAQM`. :return: A font object. - :exception IOError: If the file could not be read. + :exception OSError: If the file could not be read. """ def freetype(font): @@ -698,7 +698,7 @@ def load_path(filename): :param filename: Name of font file. :return: A font object. - :exception IOError: If the file could not be read. + :exception OSError: If the file could not be read. """ for directory in sys.path: if isDirectory(directory): diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 66e2e8560..39d5e23c7 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -60,7 +60,7 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N return im # use xdisplay=None for default display on non-win32/macOS systems if not Image.core.HAVE_XCB: - raise IOError("Pillow was built without XCB support") + raise OSError("Pillow was built without XCB support") size, data = Image.core.grabscreen_x11(xdisplay) im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1) if bbox: diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 74fb69516..4b04752ed 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1123,7 +1123,7 @@ class TiffImageFile(ImageFile.ImageFile): if hasattr(self.fp, "flush"): self.fp.flush() except OSError: - # io.BytesIO have a fileno, but returns an IOError if + # io.BytesIO have a fileno, but returns an OSError if # it doesn't use a file descriptor. fp = False @@ -1147,7 +1147,7 @@ class TiffImageFile(ImageFile.ImageFile): # underlying string for stringio. # # Rearranging for supporting byteio items, since they have a fileno - # that returns an IOError if there's no underlying fp. Easier to + # that returns an OSError if there's no underlying fp. Easier to # deal with here by reordering. if DEBUG: print("have getvalue. just sending in a string from getvalue") diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index f9cb15772..81b87e012 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -131,5 +131,5 @@ _plugins = [ ] -class UnidentifiedImageError(IOError): +class UnidentifiedImageError(OSError): pass diff --git a/src/_imaging.c b/src/_imaging.c index 0c3d766f3..9a9a51f71 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -268,9 +268,9 @@ static const char* readonly = "image is readonly"; /* static const char* no_content = "image has no content"; */ void * -ImagingError_IOError(void) +ImagingError_OSError(void) { - PyErr_SetString(PyExc_IOError, "error when accessing file"); + PyErr_SetString(PyExc_OSError, "error when accessing file"); return NULL; } diff --git a/src/_imagingcms.c b/src/_imagingcms.c index 0b22ab695..0f287013b 100644 --- a/src/_imagingcms.c +++ b/src/_imagingcms.c @@ -107,7 +107,7 @@ cms_profile_open(PyObject* self, PyObject* args) hProfile = cmsOpenProfileFromFile(sProfile, "r"); if (!hProfile) { - PyErr_SetString(PyExc_IOError, "cannot open profile file"); + PyErr_SetString(PyExc_OSError, "cannot open profile file"); return NULL; } @@ -126,7 +126,7 @@ cms_profile_fromstring(PyObject* self, PyObject* args) hProfile = cmsOpenProfileFromMem(pProfile, nProfile); if (!hProfile) { - PyErr_SetString(PyExc_IOError, "cannot open profile from string"); + PyErr_SetString(PyExc_OSError, "cannot open profile from string"); return NULL; } @@ -150,18 +150,18 @@ cms_profile_tobytes(PyObject* self, PyObject* args) profile = ((CmsProfileObject*)CmsProfile)->profile; if (!cmsSaveProfileToMem(profile, pProfile, &nProfile)) { - PyErr_SetString(PyExc_IOError, "Could not determine profile size"); + PyErr_SetString(PyExc_OSError, "Could not determine profile size"); return NULL; } pProfile = (char*)malloc(nProfile); if (!pProfile) { - PyErr_SetString(PyExc_IOError, "Out of Memory"); + PyErr_SetString(PyExc_OSError, "Out of Memory"); return NULL; } if (!cmsSaveProfileToMem(profile, pProfile, &nProfile)) { - PyErr_SetString(PyExc_IOError, "Could not get profile"); + PyErr_SetString(PyExc_OSError, "Could not get profile"); free(pProfile); return NULL; } @@ -655,7 +655,7 @@ _profile_read_mlu(CmsProfileObject* self, cmsTagSignature info) buf = malloc(len); if (!buf) { - PyErr_SetString(PyExc_IOError, "Out of Memory"); + PyErr_SetString(PyExc_OSError, "Out of Memory"); return NULL; } /* Just in case the next call fails. */ diff --git a/src/_imagingft.c b/src/_imagingft.c index f6a5b7d59..f1b299d91 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -139,11 +139,11 @@ geterror(int code) for (i = 0; ft_errors[i].message; i++) if (ft_errors[i].code == code) { - PyErr_SetString(PyExc_IOError, ft_errors[i].message); + PyErr_SetString(PyExc_OSError, ft_errors[i].message); return NULL; } - PyErr_SetString(PyExc_IOError, "unknown freetype error"); + PyErr_SetString(PyExc_OSError, "unknown freetype error"); return NULL; } @@ -259,7 +259,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) if (!library) { PyErr_SetString( - PyExc_IOError, + PyExc_OSError, "failed to initialize FreeType library" ); return NULL; diff --git a/src/_webp.c b/src/_webp.c index 93cf7ae85..babea60f3 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -70,7 +70,7 @@ PyObject* HandleMuxError(WebPMuxError err, char* chunk) { case WEBP_MUX_BAD_DATA: case WEBP_MUX_NOT_ENOUGH_DATA: - PyErr_SetString(PyExc_IOError, message); + PyErr_SetString(PyExc_OSError, message); break; default: @@ -423,7 +423,7 @@ PyObject* _anim_decoder_get_next(PyObject* self) WebPAnimDecoderObject* decp = (WebPAnimDecoderObject*)self; if (!WebPAnimDecoderGetNext(decp->dec, &buf, ×tamp)) { - PyErr_SetString(PyExc_IOError, "failed to read next frame"); + PyErr_SetString(PyExc_OSError, "failed to read next frame"); return NULL; } diff --git a/src/display.c b/src/display.c index 21869b26e..eff9de5c4 100644 --- a/src/display.c +++ b/src/display.c @@ -158,7 +158,7 @@ _getdc(ImagingDisplayObject* display, PyObject* args) dc = GetDC(window); if (!dc) { - PyErr_SetString(PyExc_IOError, "cannot create dc"); + PyErr_SetString(PyExc_OSError, "cannot create dc"); return NULL; } @@ -397,7 +397,7 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args) return Py_BuildValue("(ii)(ii)N", x, y, width, height, buffer); error: - PyErr_SetString(PyExc_IOError, "screen grab failed"); + PyErr_SetString(PyExc_OSError, "screen grab failed"); DeleteDC(screen_copy); DeleteDC(screen); @@ -677,7 +677,7 @@ PyImaging_CreateWindowWin32(PyObject* self, PyObject* args) ); if (!wnd) { - PyErr_SetString(PyExc_IOError, "failed to create window"); + PyErr_SetString(PyExc_OSError, "failed to create window"); return NULL; } @@ -755,7 +755,7 @@ PyImaging_DrawWmf(PyObject* self, PyObject* args) } if (!meta) { - PyErr_SetString(PyExc_IOError, "cannot load metafile"); + PyErr_SetString(PyExc_OSError, "cannot load metafile"); return NULL; } @@ -774,12 +774,12 @@ PyImaging_DrawWmf(PyObject* self, PyObject* args) ); if (!bitmap) { - PyErr_SetString(PyExc_IOError, "cannot create bitmap"); + PyErr_SetString(PyExc_OSError, "cannot create bitmap"); goto error; } if (!SelectObject(dc, bitmap)) { - PyErr_SetString(PyExc_IOError, "cannot select bitmap"); + PyErr_SetString(PyExc_OSError, "cannot select bitmap"); goto error; } @@ -793,7 +793,7 @@ PyImaging_DrawWmf(PyObject* self, PyObject* args) FillRect(dc, &rect, GetStockObject(WHITE_BRUSH)); if (!PlayEnhMetaFile(dc, meta, &rect)) { - PyErr_SetString(PyExc_IOError, "cannot render metafile"); + PyErr_SetString(PyExc_OSError, "cannot render metafile"); goto error; } @@ -845,7 +845,7 @@ PyImaging_GrabScreenX11(PyObject* self, PyObject* args) connection = xcb_connect(display_name, &screen_number); if (xcb_connection_has_error(connection)) { - PyErr_Format(PyExc_IOError, "X connection failed: error %i", xcb_connection_has_error(connection)); + PyErr_Format(PyExc_OSError, "X connection failed: error %i", xcb_connection_has_error(connection)); xcb_disconnect(connection); return NULL; } @@ -860,7 +860,7 @@ PyImaging_GrabScreenX11(PyObject* self, PyObject* args) if (screen == NULL || screen->root == 0) { // this case is usually caught with "X connection failed: error 6" above xcb_disconnect(connection); - PyErr_SetString(PyExc_IOError, "X screen not found"); + PyErr_SetString(PyExc_OSError, "X screen not found"); return NULL; } @@ -874,7 +874,7 @@ PyImaging_GrabScreenX11(PyObject* self, PyObject* args) 0, 0, width, height, 0x00ffffff), &error); if (reply == NULL) { - PyErr_Format(PyExc_IOError, "X get_image failed: error %i (%i, %i, %i)", + PyErr_Format(PyExc_OSError, "X get_image failed: error %i (%i, %i, %i)", error->error_code, error->major_code, error->minor_code, error->resource_id); free(error); xcb_disconnect(connection); @@ -887,7 +887,7 @@ PyImaging_GrabScreenX11(PyObject* self, PyObject* args) buffer = PyBytes_FromStringAndSize((char*)xcb_get_image_data(reply), xcb_get_image_data_length(reply)); } else { - PyErr_Format(PyExc_IOError, "unsupported bit depth: %i", reply->depth); + PyErr_Format(PyExc_OSError, "unsupported bit depth: %i", reply->depth); } free(reply); diff --git a/src/encode.c b/src/encode.c index 41ba124c4..cbf1da3a9 100644 --- a/src/encode.c +++ b/src/encode.c @@ -201,7 +201,7 @@ _encode_to_file(ImagingEncoderObject* encoder, PyObject* args) if (write(fh, buf, status) < 0) { ImagingSectionLeave(&cookie); free(buf); - return PyErr_SetFromErrno(PyExc_IOError); + return PyErr_SetFromErrno(PyExc_OSError); } } while (encoder->state.errcode == 0); diff --git a/src/libImaging/Except.c b/src/libImaging/Except.c index 515d85d1f..d6d2b2186 100644 --- a/src/libImaging/Except.c +++ b/src/libImaging/Except.c @@ -26,7 +26,7 @@ /* exception state */ void * -ImagingError_IOError(void) +ImagingError_OSError(void) { fprintf(stderr, "*** exception: file access error\n"); return NULL; diff --git a/src/libImaging/File.c b/src/libImaging/File.c index 6f014c1f8..117af9e35 100644 --- a/src/libImaging/File.c +++ b/src/libImaging/File.c @@ -59,7 +59,7 @@ ImagingSavePPM(Imaging im, const char* outfile) fp = fopen(outfile, "wb"); if (!fp) { - (void) ImagingError_IOError(); + (void) ImagingError_OSError(); return 0; } diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index 9032fcf07..47f577139 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -229,7 +229,7 @@ extern void ImagingSectionLeave(ImagingSectionCookie* cookie); /* Exceptions */ /* ---------- */ -extern void* ImagingError_IOError(void); +extern void* ImagingError_OSError(void); extern void* ImagingError_MemoryError(void); extern void* ImagingError_ModeError(void); /* maps to ValueError by default */ extern void* ImagingError_Mismatch(void); /* maps to ValueError by default */ diff --git a/src/map.c b/src/map.c index 54e0fdb22..47cc9d6de 100644 --- a/src/map.c +++ b/src/map.c @@ -70,7 +70,7 @@ PyImaging_MapperNew(const char* filename, int readonly) FILE_ATTRIBUTE_NORMAL, NULL); if (mapper->hFile == (HANDLE)-1) { - PyErr_SetString(PyExc_IOError, "cannot open file"); + PyErr_SetString(PyExc_OSError, "cannot open file"); Py_DECREF(mapper); return NULL; } @@ -81,7 +81,7 @@ PyImaging_MapperNew(const char* filename, int readonly) 0, 0, NULL); if (mapper->hMap == (HANDLE)-1) { CloseHandle(mapper->hFile); - PyErr_SetString(PyExc_IOError, "cannot map file"); + PyErr_SetString(PyExc_OSError, "cannot map file"); Py_DECREF(mapper); return NULL; } @@ -209,7 +209,7 @@ mapping_readimage(ImagingMapperObject* mapper, PyObject* args) size = ysize * stride; if (mapper->offset + size > mapper->size) { - PyErr_SetString(PyExc_IOError, "image file truncated"); + PyErr_SetString(PyExc_OSError, "image file truncated"); return NULL; }