mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-03-30 06:54:13 +03:00
Merge pull request #4302 from radarhere/stringio
Raise ValueError for io.StringIO in Image.open
This commit is contained in:
commit
e5d48969d4
|
@ -1,3 +1,4 @@
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -91,6 +92,9 @@ class TestImage(PillowTestCase):
|
||||||
def test_bad_mode(self):
|
def test_bad_mode(self):
|
||||||
self.assertRaises(ValueError, Image.open, "filename", "bad mode")
|
self.assertRaises(ValueError, Image.open, "filename", "bad mode")
|
||||||
|
|
||||||
|
def test_stringio(self):
|
||||||
|
self.assertRaises(ValueError, Image.open, io.StringIO())
|
||||||
|
|
||||||
def test_pathlib(self):
|
def test_pathlib(self):
|
||||||
from PIL.Image import Path
|
from PIL.Image import Path
|
||||||
|
|
||||||
|
|
|
@ -2690,10 +2690,17 @@ def open(fp, mode="r"):
|
||||||
:exception FileNotFoundError: If the file cannot be found.
|
:exception FileNotFoundError: If the file cannot be found.
|
||||||
:exception PIL.UnidentifiedImageError: If the image cannot be opened and
|
:exception PIL.UnidentifiedImageError: If the image cannot be opened and
|
||||||
identified.
|
identified.
|
||||||
|
:exception ValueError: If the ``mode`` is not "r", or if a ``StringIO``
|
||||||
|
instance is used for ``fp``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if mode != "r":
|
if mode != "r":
|
||||||
raise ValueError("bad mode %r" % mode)
|
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
|
exclusive_fp = False
|
||||||
filename = ""
|
filename = ""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user