-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix reading/writing from urllib.request objects #5910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,7 +206,17 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds) | |
goto exit; | ||
} | ||
buff.cursor = 0; | ||
} else if ((fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset))) { | ||
} else { | ||
#if PY3K | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just remembered seeing an article recently imploring that we stop writing if-statements like this that would possibly break in py4.x. The logic really should be checking whether or not it is py2.x, as py2.x should be considered the exception to the rule. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope -- this won't break. Here's the definition of it in mpl_utils.h: #if PY_MAJOR_VERSION >= 3
#define PY3K 1
#define Py_TPFLAGS_HAVE_NEWBUFFER 0
#else
#define PY3K 0
#endif I take |
||
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(); | ||
|
@@ -374,10 +384,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); | ||
|
@@ -387,14 +410,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); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't this need a @attr('network') decorator too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. I forgot we had that.