Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9758da5

Browse filesBrowse files
committed
Fix reading/writing from urllib.request objects
1 parent 25590c8 commit 9758da5
Copy full SHA for 9758da5

File tree

Expand file treeCollapse file tree

2 files changed

+32
-10
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-10
lines changed

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ def test_minimized_rasterized():
517517
assert False
518518

519519

520+
@cleanup
521+
def test_load_from_url():
522+
req = six.moves.urllib.request.urlopen(
523+
"http://matplotlib.org/_static/logo_sidebar_horiz.png")
524+
Z = plt.imread(req)
525+
526+
520527
if __name__=='__main__':
521528
import nose
522529
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

‎src/_png.cpp

Copy file name to clipboardExpand all lines: src/_png.cpp
+25-10Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,17 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
206206
goto exit;
207207
}
208208
buff.cursor = 0;
209-
} else if ((fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset))) {
209+
} else {
210+
#if PY3K
211+
if (close_file) {
212+
#else
213+
if (close_file || PyFile_Check(py_file)) {
214+
#endif
215+
fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset);
216+
}
217+
}
218+
219+
if (fp) {
210220
close_dup_file = true;
211221
} else {
212222
PyErr_Clear();
@@ -374,10 +384,23 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
374384
py_file = filein;
375385
}
376386

377-
if ((fp = mpl_PyFile_Dup(py_file, (char *)"rb", &offset))) {
387+
#if PY3K
388+
if (close_file) {
389+
#else
390+
if (close_file || PyFile_Check(py_file)) {
391+
#endif
392+
fp = mpl_PyFile_Dup(py_file, (char *)"rb", &offset);
393+
}
394+
395+
if (fp) {
378396
close_dup_file = true;
397+
if (fread(header, 1, 8, fp) != 8) {
398+
PyErr_SetString(PyExc_IOError, "error reading PNG header");
399+
goto exit;
400+
}
379401
} else {
380402
PyErr_Clear();
403+
381404
PyObject *read_method = PyObject_GetAttrString(py_file, "read");
382405
if (!(read_method && PyCallable_Check(read_method))) {
383406
Py_XDECREF(read_method);
@@ -387,14 +410,6 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
387410
goto exit;
388411
}
389412
Py_XDECREF(read_method);
390-
}
391-
392-
if (fp) {
393-
if (fread(header, 1, 8, fp) != 8) {
394-
PyErr_SetString(PyExc_IOError, "error reading PNG header");
395-
goto exit;
396-
}
397-
} else {
398413
_read_png_data(py_file, header, 8);
399414
}
400415

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.