Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ValueError for io.StringIO in Image.open #4302

Merged
merged 4 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import os
import shutil
import tempfile
Expand Down Expand Up @@ -91,6 +92,9 @@ def test_invalid_image(self):
def test_bad_mode(self):
self.assertRaises(ValueError, Image.open, "filename", "bad mode")

def test_stringio(self):
self.assertRaises(ValueError, Image.open, io.StringIO())

def test_pathlib(self):
from PIL.Image import Path

Expand Down
7 changes: 7 additions & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2690,10 +2690,17 @@ def open(fp, mode="r"):
:exception FileNotFoundError: If the file cannot be found.
:exception PIL.UnidentifiedImageError: If the image cannot be opened and
identified.
:exception ValueError: If the ``mode`` is not "r", or if a ``StringIO``
instance is used for ``fp``.
"""

if mode != "r":
raise ValueError("bad mode %r" % mode)
elif isinstance(fp, io.StringIO):
raise ValueError(
"StringIO cannot be used to open an image. "
"Binary data must be used instead."
)

exclusive_fp = False
filename = ""
Expand Down