Skip to content

Commit

Permalink
Merge pull request #1 from radarhere/fix_jpeg_magic_number
Browse files Browse the repository at this point in the history
Fix JPEG magic number
  • Loading branch information
Cykooz committed Jun 22, 2020
2 parents f99e0b8 + 65742cf commit cebaba1
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from io import BytesIO

import pytest
from PIL import ExifTags, Image, ImageFile, JpegImagePlugin
from PIL import ExifTags, Image, ImageFile, JpegImagePlugin, UnidentifiedImageError

from .helper import (
assert_image,
Expand Down Expand Up @@ -706,8 +706,8 @@ def test_icc_after_SOF(self):
with Image.open("Tests/images/icc-after-SOF.jpg") as im:
assert im.info["icc_profile"] == b"profile"

def test_reading_not_whole_file_for_define_it_type(self):
size = 1024 ** 2
def test_jpeg_magic_number(self):
size = 4097
buffer = BytesIO(b"\xFF" * size) # Many xFF bytes
buffer.max_pos = 0
orig_read = buffer.read
Expand All @@ -718,13 +718,11 @@ def read(n=-1):
return res

buffer.read = read
with pytest.raises(OSError):
with pytest.raises(UnidentifiedImageError):
Image.open(buffer)

# Only small part of file has been read.
# The upper limit of max_pos (8Kb) was chosen experimentally
# and increased approximately twice.
assert 0 < buffer.max_pos < 8 * 1024
# Assert the entire file has not been read
assert 0 < buffer.max_pos < size


@pytest.mark.skipif(not is_win32(), reason="Windows only")
Expand Down

0 comments on commit cebaba1

Please sign in to comment.