8000 BUG: Fix ndarray.tofile large file corruption in append mode. by charris · Pull Request #8339 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix ndarray.tofile large file corruption in append mode. #8339

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

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion numpy/core/src/multiarray/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@ npy_fallocate(npy_intp nbytes, FILE * fp)
if (nbytes < 16 * 1024 * 1024) {
return 0;
}

/* btrfs can take a while to allocate making release worthwhile */
NPY_BEGIN_ALLOW_THREADS;
r = fallocate(fileno(fp), 0, npy_ftell(fp), nbytes);
/*
* flush in case there might be some unexpected interactions between the
* fallocate call and unwritten data in the descriptor
*/
fflush(fp);
/*
* the flag "1" (=FALLOC_FL_KEEP_SIZE) is needed for the case of files
* opened in append mode (issue #8329)
*/
r = fallocate(fileno(fp), 1, npy_ftell(fp), nbytes);
NPY_END_ALLOW_THREADS;

/*
* early exit on no space, other errors will also get found during fwrite
*/
Expand Down
13 changes: 11 additions & 2 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3843,6 +3843,15 @@ def test_largish_file(self):
f.seek(d.nbytes)
d.tofile(f)
assert_equal(os.path.getsize(self.filename), d.nbytes * 2)
# check append mode (gh-8329)
open(self.filename, "w").close() # delete file contents
with open(self.filename, "ab") as f:
d.tofile(f)
assert_array_equal(d, np.fromfile(self.filename))
with open(self.filename, "ab") as f:
d.tofile(f)
assert_equal(os.path.getsize(self.filename), d.nbytes * 2)


def test_file_position_after_fromfile(self):
# gh-4118
Expand Down Expand Up @@ -4106,7 +4115,7 @@ def test_int_shape(self):
x = np.eye(3)
if IS_PYPY:
x.resize(3, refcheck=False)
else:
else:
x.resize(3)
assert_array_equal(x, np.eye(3)[0,:])

Expand Down Expand Up @@ -4135,7 +4144,7 @@ def test_zeros_appended(self):
x = np.eye(3)
if IS_PYPY:
x.resize(2, 3, 3, refcheck=False)
else:
else:
x.resize(2, 3, 3)
assert_array_equal(x[0], np.eye(3))
assert_array_equal(x[1], np.zeros((3, 3)))
Expand Down
0