diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 08654ab6c810..0bcb0f4157e1 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -6,6 +6,8 @@ import io import os +from nose.plugins.attrib import attr + import numpy as np from matplotlib.testing.decorators import (image_comparison, @@ -499,6 +501,13 @@ def test_minimized_rasterized(): assert False +@attr('network') +def test_load_from_url(): + req = six.moves.urllib.request.urlopen( + "http://matplotlib.org/_static/logo_sidebar_horiz.png") + Z = plt.imread(req) + + if __name__=='__main__': import nose nose.runmodule(argv=['-s','--with-doctest'], exit=False) diff --git a/src/_png.cpp b/src/_png.cpp index 3121edbb712d..05ac9b28960c 100644 --- a/src/_png.cpp +++ b/src/_png.cpp @@ -131,7 +131,15 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds) py_file = filein; } - if ((fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset))) { + #if PY3K + if (close_file) { + #else + if (close_file || PyFile_Check(py_file)) { + #endif + fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset); + } + + if (fp) { close_dup_file = true; } else { PyErr_Clear(); @@ -285,10 +293,23 @@ static PyObject *_read_png(PyObject *filein, bool float_result) py_file = filein; } - if ((fp = mpl_PyFile_Dup(py_file, (char *)"rb", &offset))) { + #if PY3K + if (close_file) { + #else + if (close_file || PyFile_Check(py_file)) { + #endif + fp = mpl_PyFile_Dup(py_file, (char *)"rb", &offset); + } + + if (fp) { close_dup_file = true; + if (fread(header, 1, 8, fp) != 8) { + PyErr_SetString(PyExc_IOError, "error reading PNG header"); + goto exit; + } } else { PyErr_Clear(); + PyObject *read_method = PyObject_GetAttrString(py_file, "read"); if (!(read_method && PyCallable_Check(read_method))) { Py_XDECREF(read_method); @@ -298,14 +319,6 @@ static PyObject *_read_png(PyObject *filein, bool float_result) goto exit; } Py_XDECREF(read_method); - } - - if (fp) { - if (fread(header, 1, 8, fp) != 8) { - PyErr_SetString(PyExc_IOError, "error reading PNG header"); - goto exit; - } - } else { _read_png_data(py_file, header, 8); }