Raise ValueError for io.StringIO in Image.open

This commit is contained in:
Andrew Murray 2019-12-26 13:10:39 +11:00
parent bbaebe0d20
commit fedb0407b4
2 changed files with 10 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import io
import os
import shutil
import tempfile
@ -91,6 +92,9 @@ class TestImage(PillowTestCase):
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

View File

@ -2690,10 +2690,16 @@ 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 = ""