mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-09 14:54:46 +03:00
No-seek implementation of HttpResponse Image.open
This is an alternative implementation for `Image.open()` to support opening of images from HttpResponse objects. It uses `.seekable()` to determine whether the file supports seeking (with failover for this attribute not being supported e.g. in `urllib2` response objects). The prefix is chopped off as normal and pre-pended back onto the bytes fed into the `io.IOBytes` object. This avoids loading the image object totally (duplicating it in memory) until we are sure we have a factory for the file. Add AttributeError exception catch; add io.IOBytes wrapper only once Add callable .seek test for Python2.x
This commit is contained in:
parent
174d9ac083
commit
dc23e64d6e
14
PIL/Image.py
14
PIL/Image.py
|
@ -2250,9 +2250,11 @@ def open(fp, mode="r"):
|
||||||
filename = ""
|
filename = ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fp.seek(0)
|
requires_iobytes_wrapper = not fp.seekable()
|
||||||
except (AttributeError, io.UnsupportedOperation):
|
except AttributeError:
|
||||||
fp = io.BytesIO(fp.read())
|
# Not a subclass of io.IOBase; probably Python 2.7 file (or urllib object)
|
||||||
|
# Check we have a seek fn on the object
|
||||||
|
requires_iobytes_wrapper = not callable( getattr(fp, "seek", None) )
|
||||||
|
|
||||||
prefix = fp.read(16)
|
prefix = fp.read(16)
|
||||||
|
|
||||||
|
@ -2262,6 +2264,9 @@ def open(fp, mode="r"):
|
||||||
try:
|
try:
|
||||||
factory, accept = OPEN[i]
|
factory, accept = OPEN[i]
|
||||||
if not accept or accept(prefix):
|
if not accept or accept(prefix):
|
||||||
|
if requires_iobytes_wrapper:
|
||||||
|
fp = io.BytesIO(prefix + fp.read())
|
||||||
|
requires_iobytes_wrapper = False # Wrapping once is enough
|
||||||
fp.seek(0)
|
fp.seek(0)
|
||||||
im = factory(fp, filename)
|
im = factory(fp, filename)
|
||||||
_decompression_bomb_check(im.size)
|
_decompression_bomb_check(im.size)
|
||||||
|
@ -2277,6 +2282,9 @@ def open(fp, mode="r"):
|
||||||
try:
|
try:
|
||||||
factory, accept = OPEN[i]
|
factory, accept = OPEN[i]
|
||||||
if not accept or accept(prefix):
|
if not accept or accept(prefix):
|
||||||
|
if requires_iobytes_wrapper:
|
||||||
|
fp = io.BytesIO(prefix + fp.read())
|
||||||
|
requires_iobytes_wrapper = False # Wrapping once is enough
|
||||||
fp.seek(0)
|
fp.seek(0)
|
||||||
im = factory(fp, filename)
|
im = factory(fp, filename)
|
||||||
_decompression_bomb_check(im.size)
|
_decompression_bomb_check(im.size)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user