Skip to content

Commit 99ad89d

Browse files
committed
Merge pull request #5927 from WeatherGod/backport_5910
Merge pull request #5910 from mdboom/image-read-from-url
2 parents dc3f061 + 0ea4ec9 commit 99ad89d

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/matplotlib/tests/test_image.py

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import io
77
import os
88

9+
from nose.plugins.attrib import attr
10+
911
import numpy as np
1012

1113
from matplotlib.testing.decorators import (image_comparison,
@@ -499,6 +501,13 @@ def test_minimized_rasterized():
499501
assert False
500502

501503

504+
@attr('network')
505+
def test_load_from_url():
506+
req = six.moves.urllib.request.urlopen(
507+
"http://matplotlib.org/_static/logo_sidebar_horiz.png")
508+
Z = plt.imread(req)
509+
510+
502511
if __name__=='__main__':
503512
import nose
504513
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

src/_png.cpp

+23-10
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,15 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
131131
py_file = filein;
132132
}
133133

134-
if ((fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset))) {
134+
#if PY3K
135+
if (close_file) {
136+
#else
137+
if (close_file || PyFile_Check(py_file)) {
138+
#endif
139+
fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset);
140+
}
141+
142+
if (fp) {
135143
close_dup_file = true;
136144
} else {
137145
PyErr_Clear();
@@ -285,10 +293,23 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
285293
py_file = filein;
286294
}
287295

288-
if ((fp = mpl_PyFile_Dup(py_file, (char *)"rb", &offset))) {
296+
#if PY3K
297+
if (close_file) {
298+
#else
299+
if (close_file || PyFile_Check(py_file)) {
300+
#endif
301+
fp = mpl_PyFile_Dup(py_file, (char *)"rb", &offset);
302+
}
303+
304+
if (fp) {
289305
close_dup_file = true;
306+
if (fread(header, 1, 8, fp) != 8) {
307+
PyErr_SetString(PyExc_IOError, "error reading PNG header");
308+
goto exit;
309+
}
290310
} else {
291311
PyErr_Clear();
312+
292313
PyObject *read_method = PyObject_GetAttrString(py_file, "read");
293314
if (!(read_method && PyCallable_Check(read_method))) {
294315
Py_XDECREF(read_method);
@@ -298,14 +319,6 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
298319
goto exit;
299320
}
300321
Py_XDECREF(read_method);
301-
}
302-
303-
if (fp) {
304-
if (fread(header, 1, 8, fp) != 8) {
305-
PyErr_SetString(PyExc_IOError, "error reading PNG header");
306-
goto exit;
307-
}
308-
} else {
309322
_read_png_data(py_file, header, 8);
310323
}
311324

0 commit comments

Comments
 (0)