Skip to content

Commit 5da2d9d

Browse files
committed
Merge remote-tracking branch 'upstream/v1.2.x'
2 parents 39384a5 + 293d42b commit 5da2d9d

File tree

12 files changed

+233
-87
lines changed

12 files changed

+233
-87
lines changed

examples/animation/old_animation/draggable_legend.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
2222

23-
fn = get_sample_data("lena.png", asfileobj=False)
24-
arr_lena = read_png(fn)
23+
fn = get_sample_data("ada.png", asfileobj=False)
24+
arr_ada = read_png(fn)
2525

26-
imagebox = OffsetImage(arr_lena, zoom=0.2)
26+
imagebox = OffsetImage(arr_ada, zoom=0.2)
2727

2828
ab = AnnotationBbox(imagebox, xy,
2929
xybox=(120., -80.),

examples/misc/sample_data_demo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import print_function
66
import matplotlib.cbook as cbook
77
import matplotlib.pyplot as plt
8-
fname = cbook.get_sample_data('lena.png', asfileobj=False)
8+
fname = cbook.get_sample_data('ada.png', asfileobj=False)
99

1010
print('fname', fname)
1111
im = plt.imread(fname)

examples/pylab_examples/demo_annotation_box.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070

7171
from matplotlib._png import read_png
72-
fn = get_sample_data("lena.png", asfileobj=False)
72+
fn = get_sample_data("grace_hopper.png", asfileobj=False)
7373
arr_lena = read_png(fn)
7474

7575
imagebox = OffsetImage(arr_lena, zoom=0.2)

examples/pylab_examples/demo_text_path.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def draw(self, renderer=None):
6363
ax = plt.subplot(211)
6464

6565
from matplotlib._png import read_png
66-
fn = get_sample_data("lena.png", asfileobj=False)
66+
fn = get_sample_data("grace_hopper.png", asfileobj=False)
6767
arr = read_png(fn)
6868

6969
text_path = TextPath((0, 0), "!?", size=150)

examples/pylab_examples/image_demo3.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import matplotlib.cbook as cbook
99

10-
datafile = cbook.get_sample_data('lena.jpg')
10+
datafile = cbook.get_sample_data('grace_hopper.jpg')
1111
lena = Image.open(datafile)
1212
dpi = rcParams['figure.dpi']
1313
figsize = lena.size[0]/dpi, lena.size[1]/dpi
301 KB
Loading
Loading
Loading

lib/mpl_toolkits/mplot3d/axis3d.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ def get_rotate_label(self, text):
161161

162162
def _get_coord_info(self, renderer):
163163
minx, maxx, miny, maxy, minz, maxz = self.axes.get_w_lims()
164+
if minx > maxx:
165+
minx, maxx = maxx, minx
166+
if miny > maxy:
167+
miny, maxy = maxy, miny
168+
if minz > maxz:
169+
minz, maxz = maxz, minz
164170
mins = np.array((minx, miny, minz))
165171
maxs = np.array((maxx, maxy, maxz))
166172
centers = (maxs + mins) / 2.
@@ -205,9 +211,13 @@ def draw(self, renderer):
205211
index = info['i']
206212

207213
# filter locations here so that no extra grid lines are drawn
208-
interval = self.get_view_interval()
209-
majorLocs = [loc for loc in majorLocs if \
210-
interval[0] <= loc <= interval[1]]
214+
locmin, locmax = self.get_view_interval()
215+
if locmin > locmax:
216+
locmin, locmax = locmax, locmin
217+
218+
# Rudimentary clipping
219+
majorLocs = [loc for loc in majorLocs if
220+
locmin <= loc <= locmax]
211221
self.major.formatter.set_locs(majorLocs)
212222
majorLabels = [self.major.formatter(val, i)
213223
for i, val in enumerate(majorLocs)]

src/_backend_agg.cpp

+24-25
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include "numpy/arrayobject.h"
4141
#include "agg_py_transforms.h"
42+
#include "file_compat.h"
4243

4344
#ifndef M_PI
4445
#define M_PI 3.14159265358979323846
@@ -2028,44 +2029,42 @@ RendererAgg::write_rgba(const Py::Tuple& args)
20282029

20292030
FILE *fp = NULL;
20302031
Py::Object py_fileobj = Py::Object(args[0]);
2031-
2032-
#if PY3K
2033-
int fd = PyObject_AsFileDescriptor(py_fileobj.ptr());
2034-
PyErr_Clear();
2035-
#endif
2032+
PyObject* py_file = NULL;
2033+
bool close_file = false;
20362034

20372035
if (py_fileobj.isString())
20382036
{
2039-
std::string fileName = Py::String(py_fileobj);
2040-
const char *file_name = fileName.c_str();
2041-
if ((fp = fopen(file_name, "wb")) == NULL)
2042-
throw Py::RuntimeError(
2043-
Printf("Could not open file %s", file_name).str());
2044-
if (fwrite(pixBuffer, 1, NUMBYTES, fp) != NUMBYTES)
2045-
{
2046-
fclose(fp);
2047-
throw Py::RuntimeError(
2048-
Printf("Error writing to file %s", file_name).str());
2037+
if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"wb")) == NULL) {
2038+
throw Py::Exception();
20492039
}
2040+
close_file = true;
20502041
}
2051-
#if PY3K
2052-
else if (fd != -1)
2042+
else
20532043
{
2054-
if (write(fd, pixBuffer, NUMBYTES) != (ssize_t)NUMBYTES)
2055-
{
2056-
throw Py::RuntimeError("Error writing to file");
2057-
}
2044+
py_file = py_fileobj.ptr();
20582045
}
2059-
#else
2060-
else if (PyFile_CheckExact(py_fileobj.ptr()))
2046+
2047+
if ((fp = npy_PyFile_Dup(py_file, (char *)"wb")))
20612048
{
2062-
fp = PyFile_AsFile(py_fileobj.ptr());
20632049
if (fwrite(pixBuffer, 1, NUMBYTES, fp) != NUMBYTES)
20642050
{
2051+
npy_PyFile_DupClose(py_file, fp);
2052+
2053+
if (close_file) {
2054+
npy_PyFile_CloseFile(py_file);
2055+
Py_DECREF(py_file);
2056+
}
2057+
20652058
throw Py::RuntimeError("Error writing to file");
20662059
}
2060+
2061+
npy_PyFile_DupClose(py_file, fp);
2062+
2063+
if (close_file) {
2064+
npy_PyFile_CloseFile(py_file);
2065+
Py_DECREF(py_file);
2066+
}
20672067
}
2068-
#endif
20692068
else
20702069
{
20712070
PyObject* write_method = PyObject_GetAttrString(py_fileobj.ptr(),

src/_png.cpp

+54-52
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "CXX/Extensions.hxx"
2828
#include "numpy/arrayobject.h"
2929
#include "mplutils.h"
30+
#include "file_compat.h"
3031

3132
// As reported in [3082058] build _png.so on aix
3233
#ifdef _AIX
@@ -104,6 +105,7 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
104105

105106
FILE *fp = NULL;
106107
bool close_file = false;
108+
bool close_dup_file = false;
107109
Py::Object buffer_obj = Py::Object(args[0]);
108110
PyObject* buffer = buffer_obj.ptr();
109111
if (!PyObject_CheckReadBuffer(buffer))
@@ -128,41 +130,34 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
128130
}
129131

130132
Py::Object py_fileobj = Py::Object(args[3]);
131-
#if PY3K
132-
int fd = PyObject_AsFileDescriptor(py_fileobj.ptr());
133-
PyErr_Clear();
134-
#endif
133+
PyObject* py_file = NULL;
135134
if (py_fileobj.isString())
136135
{
137-
std::string fileName = Py::String(py_fileobj);
138-
const char *file_name = fileName.c_str();
139-
if ((fp = fopen(file_name, "wb")) == NULL)
140-
{
141-
throw Py::RuntimeError(
142-
Printf("Could not open file %s", file_name).str());
136+
if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"wb")) == NULL) {
137+
throw Py::Exception();
143138
}
144139
close_file = true;
145140
}
146-
#if PY3K
147-
else if (fd != -1)
141+
else
148142
{
149-
fp = fdopen(fd, "w");
143+
py_file = py_fileobj.ptr();
150144
}
151-
#else
152-
else if (PyFile_CheckExact(py_fileobj.ptr()))
145+
146+
if ((fp = npy_PyFile_Dup(py_file, (char *)"wb")))
153147
{
154-
fp = PyFile_AsFile(py_fileobj.ptr());
148+
close_dup_file = true;
155149
}
156-
#endif
157150
else
158151
{
152+
PyErr_Clear();
159153
PyObject* write_method = PyObject_GetAttrString(
160-
py_fileobj.ptr(), "write");
154+
py_file, "write");
161155
if (!(write_method && PyCallable_Check(write_method)))
162156
{
163157
Py_XDECREF(write_method);
164158
throw Py::TypeError(
165-
"Object does not appear to be a 8-bit string path or a Python file-like object");
159+
"Object does not appear to be a 8-bit string path or "
160+
"a Python file-like object");
166161
}
167162
Py_XDECREF(write_method);
168163
}
@@ -205,7 +200,7 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
205200
}
206201
else
207202
{
208-
png_set_write_fn(png_ptr, (void*)py_fileobj.ptr(),
203+
png_set_write_fn(png_ptr, (void*)py_file,
209204
&write_png_data, &flush_png_data);
210205
}
211206
png_set_IHDR(png_ptr, info_ptr,
@@ -241,9 +236,16 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
241236
png_destroy_write_struct(&png_ptr, &info_ptr);
242237
}
243238
delete [] row_pointers;
244-
if (fp && close_file)
239+
240+
if (close_dup_file)
241+
{
242+
npy_PyFile_DupClose(py_file, fp);
243+
}
244+
245+
if (close_file)
245246
{
246-
fclose(fp);
247+
npy_PyFile_CloseFile(py_file);
248+
Py_DECREF(py_file);
247249
}
248250
/* Changed calls to png_destroy_write_struct to follow
249251
http://www.libpng.org/pub/png/libpng-manual.txt.
@@ -254,15 +256,15 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
254256

255257
png_destroy_write_struct(&png_ptr, &info_ptr);
256258
delete [] row_pointers;
257-
#if PY3K
258-
if (fp)
259+
if (close_dup_file)
259260
{
260-
fflush(fp);
261+
npy_PyFile_DupClose(py_file, fp);
261262
}
262-
#endif
263-
if (fp && close_file)
263+
264+
if (close_file)
264265
{
265-
fclose(fp);
266+
npy_PyFile_CloseFile(py_file);
267+
Py_DECREF(py_file);
266268
}
267269

268270
if (PyErr_Occurred()) {
@@ -306,40 +308,33 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
306308
png_byte header[8]; // 8 is the maximum size that can be checked
307309
FILE* fp = NULL;
308310
bool close_file = false;
309-
310-
#if PY3K
311-
int fd = PyObject_AsFileDescriptor(py_fileobj.ptr());
312-
PyErr_Clear();
313-
#endif
311+
bool close_dup_file = false;
312+
PyObject *py_file = NULL;
314313

315314
if (py_fileobj.isString())
316315
{
317-
std::string fileName = Py::String(py_fileobj);
318-
const char *file_name = fileName.c_str();
319-
if ((fp = fopen(file_name, "rb")) == NULL)
320-
{
321-
throw Py::RuntimeError(
322-
Printf("Could not open file %s for reading", file_name).str());
316+
if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"rb")) == NULL) {
317+
throw Py::Exception();
323318
}
324319
close_file = true;
320+
} else {
321+
py_file = py_fileobj.ptr();
325322
}
326-
#if PY3K
327-
else if (fd != -1) {
328-
fp = fdopen(fd, "r");
329-
}
330-
#else
331-
else if (PyFile_CheckExact(py_fileobj.ptr()))
323+
324+
if ((fp = npy_PyFile_Dup(py_file, "rb")))
332325
{
333-
fp = PyFile_AsFile(py_fileobj.ptr());
326+
close_dup_file = true;
334327
}
335-
#endif
336328
else
337329
{
338-
PyObject* read_method = PyObject_GetAttrString(py_fileobj.ptr(), "read");
330+
PyErr_Clear();
331+
PyObject* read_method = PyObject_GetAttrString(py_file, "read");
339332
if (!(read_method && PyCallable_Check(read_method)))
340333
{
341334
Py_XDECREF(read_method);
342-
throw Py::TypeError("Object does not appear to be a 8-bit string path or a Python file-like object");
335+
throw Py::TypeError(
336+
"Object does not appear to be a 8-bit string path or a Python "
337+
"file-like object");
343338
}
344339
Py_XDECREF(read_method);
345340
}
@@ -354,7 +349,7 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
354349
}
355350
else
356351
{
357-
_read_png_data(py_fileobj.ptr(), header, 8);
352+
_read_png_data(py_file, header, 8);
358353
}
359354
if (png_sig_cmp(header, 0, 8))
360355
{
@@ -390,7 +385,7 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
390385
}
391386
else
392387
{
393-
png_set_read_fn(png_ptr, (void*)py_fileobj.ptr(), &read_png_data);
388+
png_set_read_fn(png_ptr, (void*)py_file, &read_png_data);
394389
}
395390
png_set_sig_bytes(png_ptr, 8);
396391
png_read_info(png_ptr, info_ptr);
@@ -572,10 +567,17 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
572567
#else
573568
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
574569
#endif
570+
if (close_dup_file)
571+
{
572+
npy_PyFile_DupClose(py_file, fp);
573+
}
574+
575575
if (close_file)
576576
{
577-
fclose(fp);
577+
npy_PyFile_CloseFile(py_file);
578+
Py_DECREF(py_file);
578579
}
580+
579581
for (row = 0; row < height; row++)
580582
{
581583
delete [] row_pointers[row];

0 commit comments

Comments
 (0)