diff options
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 * |
