Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a17787b

Browse files
committed
TST: add test of truncated files and buffers
1 parent 07633f3 commit a17787b

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

lib/matplotlib/tests/test_png.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
unicode_literals)
33

44
import six
5-
5+
from six import BytesIO
66
import glob
77
import os
8-
8+
import shutil
99
import numpy as np
10+
import pytest
1011

1112
from matplotlib.testing.decorators import image_comparison
1213
from matplotlib import pyplot as plt
1314
import matplotlib.cm as cm
14-
15+
import tempfile
1516
import sys
1617
on_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)

0 commit comments

Comments
 (0)