From 204d11d4da15879946c1120c43e6f75b2a338d5b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 30 Jun 2025 22:29:24 +1000 Subject: [PATCH] Raise FileNotFoundError when opening an empty path --- Tests/test_image.py | 4 ++++ src/PIL/Image.py | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 069083b19..6b8b6d42b 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -160,6 +160,10 @@ class TestImage: with pytest.raises(AttributeError): im.mode = "P" # type: ignore[misc] + def test_empty_path(self) -> None: + with pytest.raises(FileNotFoundError): + Image.open("") + def test_invalid_image(self) -> None: im = io.BytesIO(b"") with pytest.raises(UnidentifiedImageError): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 9fc1c7067..d209405c4 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3510,8 +3510,6 @@ def open( filename: str | bytes = "" if is_path(fp): filename = os.fspath(fp) - - if filename: fp = builtins.open(filename, "rb") exclusive_fp = True else: