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

Skip to content

Commit 2fd479a

Browse files
committed
Merge remote-tracking branch 'matplotlib/v2.1.x'
Conflicts: lib/matplotlib/pyplot.py - identical changes to docstring lib/matplotlib/tests/test_artist.py - adjacent tests changed, kept both versions
2 parents 37e79c6 + c66c423 commit 2fd479a

File tree

6 files changed

+129
-60
lines changed

6 files changed

+129
-60
lines changed

MANIFEST.in

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
include CHANGELOG INSTALL
1+
include INSTALL.rst README.rst
22
include CONTRIBUTING.md
3+
include pytest.ini
34
include Makefile MANIFEST.in
45
include matplotlibrc.template setup.cfg.template
56
include setupext.py setup.py distribute_setup.py
@@ -10,13 +11,16 @@ include lib/matplotlib/mpl-data/fonts/ttf/*
1011
include lib/matplotlib/mpl-data/fonts/pdfcorefonts/*
1112
include lib/matplotlib/mpl-data/fonts/afm/*
1213
include lib/matplotlib/mpl-data/stylelib/*
13-
recursive-include lib/matplotlib/mpl-data/sample_data *
1414
recursive-include LICENSE *
15-
recursive-include examples *
1615
recursive-include doc *
17-
recursive-include src *.cpp *.c *.h *.m
18-
recursive-include lib *
16+
recursive-include examples *
1917
recursive-include extern *
18+
recursive-include lib *
19+
recursive-include lib/matplotlib/mpl-data/sample_data *
20+
recursive-include src *.cpp *.c *.h *.m
21+
recursive-include tools *
22+
recursive-include tutorials *
23+
recursive-include unit *
2024
include versioneer.py
2125
include lib/matplotlib/_version.py
2226
include tests.py

lib/matplotlib/artist.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ def set_zorder(self, level):
948948
..
949949
ACCEPTS: float
950950
"""
951+
if level is None:
952+
level = self.__class__.zorder
951953
self.zorder = level
952954
self.pchanged()
953955
self.stale = True

lib/matplotlib/figure.py

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -920,11 +920,12 @@ def add_axes(self, *args, **kwargs):
920920
polar : boolean, optional
921921
If True, equivalent to projection='polar'.
922922
923-
This method also takes the keyword arguments for
924-
:class:`~matplotlib.axes.Axes`.
923+
**kwargs
924+
This method also takes the keyword arguments for
925+
:class:`~matplotlib.axes.Axes`.
925926
926927
Returns
927-
------
928+
-------
928929
axes : Axes
929930
The added axes.
930931
@@ -1022,8 +1023,9 @@ def add_subplot(self, *args, **kwargs):
10221023
polar : boolean, optional
10231024
If True, equivalent to projection='polar'.
10241025
1025-
This method also takes the keyword arguments for
1026-
:class:`~matplotlib.axes.Axes`.
1026+
**kwargs
1027+
This method also takes the keyword arguments for
1028+
:class:`~matplotlib.axes.Axes`.
10271029
10281030
Returns
10291031
-------
@@ -1038,6 +1040,8 @@ def add_subplot(self, *args, **kwargs):
10381040
10391041
Examples
10401042
--------
1043+
::
1044+
10411045
fig.add_subplot(111)
10421046
10431047
# equivalent but more general
@@ -1762,9 +1766,10 @@ def savefig(self, fname, **kwargs):
17621766
17631767
The output formats available depend on the backend being used.
17641768
1765-
Arguments:
1769+
Parameters
1770+
----------
17661771
1767-
*fname*:
1772+
fname : str or file-like object
17681773
A string containing a path to a filename, or a Python
17691774
file-like object, or possibly some backend-dependent object
17701775
such as :class:`~matplotlib.backends.backend_pdf.PdfPages`.
@@ -1777,29 +1782,33 @@ def savefig(self, fname, **kwargs):
17771782
If *fname* is not a string, remember to specify *format* to
17781783
ensure that the correct backend is used.
17791784
1780-
Keyword arguments:
1785+
Other Parameters
1786+
----------------
17811787
1782-
*dpi*: [ *None* | ``scalar > 0`` | 'figure']
1788+
dpi : [ *None* | scalar > 0 | 'figure']
17831789
The resolution in dots per inch. If *None* it will default to
17841790
the value ``savefig.dpi`` in the matplotlibrc file. If 'figure'
17851791
it will set the dpi to be the value of the figure.
17861792
1787-
*facecolor*, *edgecolor*:
1788-
the colors of the figure rectangle
1793+
facecolor : color spec or None, optional
1794+
the facecolor of the figure; if None, defaults to savefig.facecolor
1795+
1796+
edgecolor : color spec or None, optional
1797+
the edgecolor of the figure; if None, defaults to savefig.edgecolor
17891798
1790-
*orientation*: [ 'landscape' | 'portrait' ]
1799+
orientation : {'landscape', 'portrait'}
17911800
not supported on all backends; currently only on postscript output
17921801
1793-
*papertype*:
1802+
papertype : str
17941803
One of 'letter', 'legal', 'executive', 'ledger', 'a0' through
17951804
'a10', 'b0' through 'b10'. Only supported for postscript
17961805
output.
17971806
1798-
*format*:
1807+
format : str
17991808
One of the file extensions supported by the active
18001809
backend. Most backends support png, pdf, ps, eps and svg.
18011810
1802-
*transparent*:
1811+
transparent : bool
18031812
If *True*, the axes patches will all be transparent; the
18041813
figure patch will also be transparent unless facecolor
18051814
and/or edgecolor are specified via kwargs.
@@ -1808,21 +1817,21 @@ def savefig(self, fname, **kwargs):
18081817
transparency of these patches will be restored to their
18091818
original values upon exit of this function.
18101819
1811-
*frameon*:
1820+
frameon : bool
18121821
If *True*, the figure patch will be colored, if *False*, the
18131822
figure background will be transparent. If not provided, the
18141823
rcParam 'savefig.frameon' will be used.
18151824
1816-
*bbox_inches*:
1825+
bbox_inches : str or `~matplotlib.transforms.Bbox`, optional
18171826
Bbox in inches. Only the given portion of the figure is
18181827
saved. If 'tight', try to figure out the tight bbox of
1819-
the figure.
1828+
the figure. If None, use savefig.bbox
18201829
1821-
*pad_inches*:
1830+
pad_inches : scalar, optional
18221831
Amount of padding around the figure when bbox_inches is
1823-
'tight'.
1832+
'tight'. If None, use savefig.pad_inches
18241833
1825-
*bbox_extra_artists*:
1834+
bbox_extra_artists : list of `~matplotlib.artist.Artist`, optional
18261835
A list of extra artists that will be considered when the
18271836
tight bbox is calculated.
18281837
@@ -2016,17 +2025,20 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
20162025
"""
20172026
Adjust subplot parameters to give specified padding.
20182027
2019-
Parameters:
2028+
Parameters
2029+
----------
20202030
2021-
pad : float
2031+
pad : float
20222032
padding between the figure edge and the edges of subplots,
20232033
as a fraction of the font-size.
2024-
h_pad, w_pad : float
2034+
2035+
h_pad, w_pad : float, optional
20252036
padding (height/width) between edges of adjacent subplots.
20262037
Defaults to `pad_inches`.
2027-
rect : if rect is given, it is interpreted as a rectangle
2028-
(left, bottom, right, top) in the normalized figure
2029-
coordinate that the whole subplots area (including
2038+
2039+
rect : tuple (left, bottom, right, top), optional
2040+
a rectangle (left, bottom, right, top) in the normalized
2041+
figure coordinate that the whole subplots area (including
20302042
labels) will fit into. Default is (0, 0, 1, 1).
20312043
"""
20322044

lib/matplotlib/tests/test_artist.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,13 @@ def test_setp():
246246
sio = io.StringIO()
247247
plt.setp(lines1, 'zorder', file=sio)
248248
assert sio.getvalue() == ' zorder: float \n'
249+
250+
251+
def test_None_zorder():
252+
fig, ax = plt.subplots()
253+
ln, = ax.plot(range(5), zorder=None)
254+
assert ln.get_zorder() == mlines.Line2D.zorder
255+
ln.set_zorder(123456)
256+
assert ln.get_zorder() == 123456
257+
ln.set_zorder(None)
258+
assert ln.get_zorder() == mlines.Line2D.zorder

lib/matplotlib/tests/test_png.py

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

44
import six
5-
5+
from six import BytesIO
66
import glob
77
import os
8-
98
import numpy as np
9+
import pytest
1010

1111
from matplotlib.testing.decorators import image_comparison
1212
from matplotlib import pyplot as plt
1313
import matplotlib.cm as cm
14-
1514
import sys
1615
on_win = (sys.platform == 'win32')
1716

@@ -46,3 +45,28 @@ def test_imread_png_uint16():
4645

4746
assert (img.dtype == np.uint16)
4847
assert np.sum(img.flatten()) == 134184960
48+
49+
50+
def test_truncated_file(tmpdir):
51+
d = tmpdir.mkdir('test')
52+
fname = str(d.join('test.png'))
53+
fname_t = str(d.join('test_truncated.png'))
54+
plt.savefig(fname)
55+
with open(fname, 'rb') as fin:
56+
buf = fin.read()
57+
with open(fname_t, 'wb') as fout:
58+
fout.write(buf[:20])
59+
60+
with pytest.raises(Exception):
61+
plt.imread(fname_t)
62+
63+
64+
def test_truncated_buffer():
65+
b = BytesIO()
66+
plt.savefig(b)
67+
b.seek(0)
68+
b2 = BytesIO(b.read(20))
69+
b2.seek(0)
70+
71+
with pytest.raises(Exception):
72+
plt.imread(b2)

src/_png.cpp

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,18 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
194194

195195
switch (channels) {
196196
case 1:
197-
png_color_type = PNG_COLOR_TYPE_GRAY;
198-
break;
197+
png_color_type = PNG_COLOR_TYPE_GRAY;
198+
break;
199199
case 3:
200-
png_color_type = PNG_COLOR_TYPE_RGB;
201-
break;
200+
png_color_type = PNG_COLOR_TYPE_RGB;
201+
break;
202202
case 4:
203-
png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
204-
break;
203+
png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
204+
break;
205205
default:
206206
PyErr_SetString(PyExc_ValueError,
207-
"Buffer must be an NxMxD array with D in 1, 3, 4 "
208-
"(grayscale, RGB, RGBA)");
207+
"Buffer must be an NxMxD array with D in 1, 3, 4 "
208+
"(grayscale, RGB, RGBA)");
209209
goto exit;
210210
}
211211

@@ -349,20 +349,20 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
349349
sig_bit.alpha = 0;
350350
switch (png_color_type) {
351351
case PNG_COLOR_TYPE_GRAY:
352-
sig_bit.gray = 8;
353-
sig_bit.red = 0;
354-
sig_bit.green = 0;
355-
sig_bit.blue = 0;
356-
break;
352+
sig_bit.gray = 8;
353+
sig_bit.red = 0;
354+
sig_bit.green = 0;
355+
sig_bit.blue = 0;
356+
break;
357357
case PNG_COLOR_TYPE_RGB_ALPHA:
358-
sig_bit.alpha = 8;
359-
// fall through
358+
sig_bit.alpha = 8;
359+
// fall through
360360
case PNG_COLOR_TYPE_RGB:
361-
sig_bit.gray = 0;
362-
sig_bit.red = 8;
363-
sig_bit.green = 8;
364-
sig_bit.blue = 8;
365-
break;
361+
sig_bit.gray = 0;
362+
sig_bit.red = 8;
363+
sig_bit.green = 8;
364+
sig_bit.blue = 8;
365+
break;
366366
default:
367367
PyErr_SetString(PyExc_RuntimeError, "internal error, bad png_color_type");
368368
goto exit;
@@ -408,13 +408,21 @@ static void _read_png_data(PyObject *py_file_obj, png_bytep data, png_size_t len
408408
Py_ssize_t bufflen;
409409
if (read_method) {
410410
result = PyObject_CallFunction(read_method, (char *)"i", length);
411-
if (PyBytes_AsStringAndSize(result, &buffer, &bufflen) == 0) {
412-
if (bufflen == (Py_ssize_t)length) {
413-
memcpy(data, buffer, length);
411+
if (result) {
412+
if (PyBytes_AsStringAndSize(result, &buffer, &bufflen) == 0) {
413+
if (bufflen == (Py_ssize_t)length) {
414+
memcpy(data, buffer, length);
415+
} else {
416+
PyErr_SetString(PyExc_IOError, "read past end of file");
417+
}
414418
} else {
415-
PyErr_SetString(PyExc_IOError, "read past end of file");
419+
PyErr_SetString(PyExc_IOError, "failed to copy buffer");
416420
}
421+
} else {
422+
PyErr_SetString(PyExc_IOError, "failed to read file");
417423
}
424+
425+
418426
}
419427
Py_XDECREF(read_method);
420428
Py_XDECREF(result);
@@ -424,6 +432,10 @@ static void read_png_data(png_structp png_ptr, png_bytep data, png_size_t length
424432
{
425433
PyObject *py_file_obj = (PyObject *)png_get_io_ptr(png_ptr);
426434
_read_png_data(py_file_obj, data, length);
435+
if (PyErr_Occurred()) {
436+
png_error(png_ptr, "failed to read file");
437+
}
438+
427439
}
428440

429441
static PyObject *_read_png(PyObject *filein, bool float_result)
@@ -481,6 +493,9 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
481493
}
482494
Py_XDECREF(read_method);
483495
_read_png_data(py_file, header, 8);
496+
if (PyErr_Occurred()) {
497+
goto exit;
498+
}
484499
}
485500

486501
if (png_sig_cmp(header, 0, 8)) {
@@ -503,7 +518,9 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
503518
}
504519

505520
if (setjmp(png_jmpbuf(png_ptr))) {
506-
PyErr_SetString(PyExc_RuntimeError, "Error setting jump");
521+
if (!PyErr_Occurred()) {
522+
PyErr_SetString(PyExc_RuntimeError, "error setting jump");
523+
}
507524
goto exit;
508525
}
509526

0 commit comments

Comments
 (0)