File tree Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -44,10 +44,21 @@ npy_fallocate(npy_intp nbytes, FILE * fp)
44
44
if (nbytes < 16 * 1024 * 1024 ) {
45
45
return 0 ;
46
46
}
47
+
47
48
/* btrfs can take a while to allocate making release worthwhile */
48
49
NPY_BEGIN_ALLOW_THREADS ;
49
- r = fallocate (fileno (fp ), 0 , npy_ftell (fp ), nbytes );
50
+ /*
51
+ * flush in case there might be some unexpected interactions between the
52
+ * fallocate call and unwritten data in the descriptor
53
+ */
54
+ fflush (fp );
55
+ /*
56
+ * the flag "1" (=FALLOC_FL_KEEP_SIZE) is needed for the case of files
57
+ * opened in append mode (issue #8329)
58
+ */
59
+ r = fallocate (fileno (fp ), 1 , npy_ftell (fp ), nbytes );
50
60
NPY_END_ALLOW_THREADS ;
61
+
51
62
/*
52
63
* early exit on no space, other errors will also get found during fwrite
53
64
*/
Original file line number Diff line number Diff line change @@ -3861,6 +3861,15 @@ def test_largish_file(self):
3861
3861
f .seek (d .nbytes )
3862
3862
d .tofile (f )
3863
3863
assert_equal (os .path .getsize (self .filename ), d .nbytes * 2 )
3864
+ # check append mode (gh-8329)
3865
+ open (self .filename , "w" ).close () # delete file contents
3866
+ with open (self .filename , "ab" ) as f :
3867
+ d .tofile (f )
3868
+ assert_array_equal (d , np .fromfile (self .filename ))
3869
+ with open (self .filename , "ab" ) as f :
3870
+ d .tofile (f )
3871
+ assert_equal (os .path .getsize (self .filename ), d .nbytes * 2 )
3872
+
3864
3873
3865
3874
def test_file_position_after_fromfile (self ):
3866
3875
# gh-4118
@@ -4124,7 +4133,7 @@ def test_int_shape(self):
4124
4133
x = np .eye (3 )
4125
4134
if IS_PYPY :
4126
4135
x .resize (3 , refcheck = False )
4127
- else :
4136
+ else :
4128
4137
x .resize (3 )
4129
4138
assert_array_equal (x , np .eye (3 )[0 ,:])
4130
4139
@@ -4153,7 +4162,7 @@ def test_zeros_appended(self):
4153
4162
x = np .eye (3 )
4154
4163
if IS_PYPY :
4155
4164
x .resize (2 , 3 , 3 , refcheck = False )
4156
- else :
4165
+ else :
4157
4166
x .resize (2 , 3 , 3 )
4158
4167
assert_array_equal (x [0 ], np .eye (3 ))
4159
4168
assert_array_equal (x [1 ], np .zeros ((3 , 3 )))
You can’t perform that action at this time.
0 commit comments