Merge pull request #4302 from radarhere/stringio

Raise ValueError for io.StringIO in Image.open
This commit is contained in:
Andrew Murray 2019-12-26 21:28:07 +11:00 committed by GitHub
commit e5d48969d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 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,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 = ""