File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 unicode_literals )
33
44import six
5-
5+ from six import BytesIO
66import glob
77import os
8-
8+ import shutil
99import numpy as np
10+ import pytest
1011
1112from matplotlib .testing .decorators import image_comparison
1213from matplotlib import pyplot as plt
1314import matplotlib .cm as cm
14-
15+ import tempfile
1516import sys
1617on_win = (sys .platform == 'win32' )
1718
@@ -46,3 +47,30 @@ def test_imread_png_uint16():
4647
4748 assert (img .dtype == np .uint16 )
4849 assert np .sum (img .flatten ()) == 134184960
50+
51+
52+ def test_truncated_file ():
53+ d = tempfile .mkdtemp ()
54+ fname = os .path .join (d , 'test.png' )
55+ fname_t = os .path .join (d , 'test_truncated.png' )
56+ plt .savefig (fname )
57+ with open (fname , 'rb' ) as fin :
58+ buf = fin .read ()
59+ with open (fname_t , 'wb' ) as fout :
60+ fout .write (buf [:20 ])
61+
62+ with pytest .raises (Exception ):
63+ plt .imread (fname_t )
64+
65+ shutil .rmtree (d )
66+
67+
68+ def test_truncated_buffer ():
69+ b = BytesIO ()
70+ plt .savefig (b )
71+ b .seek (0 )
72+ b2 = BytesIO (b .read (20 ))
73+ b2 .seek (0 )
74+
75+ with pytest .raises (Exception ):
76+ plt .imread (b2 )
You can’t perform that action at this time.
0 commit comments