diff options
author | Simon Graham <simon.graham@seequent.com> | 2020-10-22 09:27:35 +1300 |
---|---|---|
committer | Simon Graham <simon.graham@seequent.com> | 2020-10-29 15:31:06 +1300 |
commit | 05a71f1d6feafddfec38d20d2f30d0ff361811a1 (patch) | |
tree | 3bbd7a43cab6f11bf6f9e87065e4565b30b4d264 /numpy/core | |
parent | a1059a3eb71e09cf39fc9c61fb3c5af754efaa79 (diff) | |
download | numpy-05a71f1d6feafddfec38d20d2f30d0ff361811a1.tar.gz |
BUG: array_tofile now always closes the original file handle.
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 1835a770f..749959534 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -585,7 +585,7 @@ array_tostring(PyArrayObject *self, PyObject *args, PyObject *kwds) static PyObject * array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds) { - int own, res1, res2; + int own; PyObject *file; FILE *fd; char *sep = ""; @@ -617,30 +617,31 @@ array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds) fd = npy_PyFile_Dup2(file, "wb", &orig_pos); if (fd == NULL) { - goto fail; + Py_DECREF(file); + return NULL; } + int res_write = PyArray_ToFile(self, fd, sep, format); - { - PyObject *type, *value, *traceback; - PyErr_Fetch(&type, &value, &traceback); - int res_close = npy_PyFile_DupClose2(file, fd, orig_pos); - npy_PyErr_ChainExceptions(type, value, traceback); - if (res_close < 0) { - return NULL; - } - } - if (res_write < 0) { - return NULL; + PyObject *err_type, *err_value, *err_traceback; + PyErr_Fetch(&err_type, &err_value, &err_traceback); + + int res_close = npy_PyFile_DupClose2(file, fd, orig_pos); + if (res_close < 0) { + npy_PyErr_ChainExceptions(err_type, err_value, err_traceback); } + if (own && npy_PyFile_CloseFile(file) < 0) { - goto fail; + npy_PyErr_ChainExceptions(err_type, err_value, err_traceback); + res_close = -1; } - Py_DECREF(file); - Py_RETURN_NONE; -fail: + PyErr_Restore(err_type, err_value, err_traceback); Py_DECREF(file); - return NULL; + + if (res_write < 0 || res_close < 0) { + return NULL; + } + Py_RETURN_NONE; } static PyObject * |