diff options
author | Pauli Virtanen <pav@iki.fi> | 2010-09-11 16:56:14 +0000 |
---|---|---|
committer | Pauli Virtanen <pav@iki.fi> | 2010-09-11 16:56:14 +0000 |
commit | 336cd47bd08d1b54620a446aee1e546b5631d89b (patch) | |
tree | fca6c2c8538bd250aa31f98fbef734e6eaaf2373 /numpy | |
parent | b4629cce24744c9c9a85912e4cfaf78a46455c12 (diff) | |
download | numpy-336cd47bd08d1b54620a446aee1e546b5631d89b.tar.gz |
BUG: core: on Python3, seek file handle to the current position in npy_PyFile_Dup (fixing #1610)
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/include/numpy/npy_3kcompat.h | 13 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 5 |
2 files changed, 18 insertions, 0 deletions
diff --git a/numpy/core/include/numpy/npy_3kcompat.h b/numpy/core/include/numpy/npy_3kcompat.h index 185ae678f..af5ddd8c7 100644 --- a/numpy/core/include/numpy/npy_3kcompat.h +++ b/numpy/core/include/numpy/npy_3kcompat.h @@ -161,6 +161,7 @@ npy_PyFile_Dup(PyObject *file, char *mode) { int fd, fd2; PyObject *ret, *os; + Py_ssize_t pos; FILE *handle; /* Flush first to ensure things end up in the file in the correct order */ ret = PyObject_CallMethod(file, "flush", ""); @@ -192,6 +193,18 @@ npy_PyFile_Dup(PyObject *file, char *mode) PyErr_SetString(PyExc_IOError, "Getting a FILE* from a Python file object failed"); } + ret = PyObject_CallMethod(file, "tell", ""); + if (ret == NULL) { + fclose(handle); + return NULL; + } + pos = PyNumber_AsSsize_t(ret, PyExc_OverflowError); + Py_DECREF(ret); + if (PyErr_Occurred()) { + fclose(handle); + return NULL; + } + fseek(handle, pos, SEEK_SET); return handle; } diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index d5ba4849b..d9c43b496 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1393,6 +1393,11 @@ class TestRegression(TestCase): data = f.read(3) assert_equal(data, asbytes("\x01\x02\x03")) + f.seek(80) + f.read(4) + data = np.fromfile(f, dtype='u1', count=4) + assert_equal(data, np.array([84, 85, 86, 87], dtype='u1')) + f.close() if __name__ == "__main__": |