summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2012-03-10 13:27:57 -0700
committerCharles Harris <charlesr.harris@gmail.com>2012-03-12 07:46:18 -0600
commitb379fd7422257fcac463699288fb541e71240974 (patch)
tree5e2951dbfdc951f7db985d9137423bf3d05a4b6f /numpy/core
parent5eab544a97b632164a03ad887d53e92f4f9898a0 (diff)
downloadnumpy-b379fd7422257fcac463699288fb541e71240974.tar.gz
PY3: Fix tofile "ResourceWarning: unclosed file".
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/src/multiarray/methods.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index 13ef868ae..6de8b10b2 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -613,7 +613,7 @@ array_tostring(PyArrayObject *self, PyObject *args, PyObject *kwds)
static PyObject *
array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds)
{
- int ret, ret2;
+ int own;
PyObject *file;
FILE *fd;
char *sep = "";
@@ -632,25 +632,35 @@ array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds)
if (file == NULL) {
return NULL;
}
+ own = 1;
}
else {
Py_INCREF(file);
+ own = 0;
}
+
fd = npy_PyFile_Dup(file, "wb");
if (fd == NULL) {
PyErr_SetString(PyExc_IOError,
"first argument must be a string or open file");
- Py_DECREF(file);
- return NULL;
+ goto fail;
}
- ret = PyArray_ToFile(self, fd, sep, format);
- ret2 = npy_PyFile_DupClose(file, fd);
- Py_DECREF(file);
- if (ret < 0 || ret2 < 0) {
- return NULL;
+ if (PyArray_ToFile(self, fd, sep, format) < 0) {
+ goto fail;
+ }
+ if (npy_PyFile_DupClose(file, fd) < 0) {
+ goto fail;
}
+ if (own && npy_PyFile_CloseFile(file) < 0) {
+ goto fail;
+ }
+ Py_DECREF(file);
Py_INCREF(Py_None);
return Py_None;
+
+fail:
+ Py_DECREF(file);
+ return NULL;
}
static PyObject *