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 @@ -3843,6 +3843,15 @@ def test_largish_file(self):
3843
3843
f .seek (d .nbytes )
3844
3844
d .tofile (f )
3845
3845
assert_equal (os .path .getsize (self .filename ), d .nbytes * 2 )
3846
+ # check append mode (gh-8329)
3847
+ open (self .filename , "w" ).close () # delete file contents
3848
+ with open (self .filename , "ab" ) as f :
3849
+ d .tofile (f )
3850
+ assert_array_equal (d , np .fromfile (self .filename ))
3851
+ with open (self .filename , "ab" ) as f :
3852
+ d .tofile (f )
3853
+ assert_equal (os .path .getsize (self .filename ), d .nbytes * 2 )
3854
+
3846
3855
3847
3856
def test_file_position_after_fromfile (self ):
3848
3857
# gh-4118
@@ -4106,7 +4115,7 @@ def test_int_shape(self):
4106
4115
x = np .eye (3 )
4107
4116
if IS_PYPY :
4108
4117
x .resize (3 , refcheck = False )
4109
- else :
4118
+ else :
4110
4119
x .resize (3 )
4111
4120
assert_array_equal (x , np .eye (3 )[0 ,:])
4112
4121
@@ -4135,7 +4144,7 @@ def test_zeros_appended(self):
4135
4144
x = np .eye (3 )
4136
4145
if IS_PYPY :
4137
4146
x .resize (2 , 3 , 3 , refcheck = False )
4138
- else :
4147
+ else :
4139
4148
x .resize (2 , 3 , 3 )
4140
4149
assert_array_equal (x [0 ], np .eye (3 ))
4141
4150
assert_array_equal (x [1 ], np .zeros ((3 , 3 )))
You can’t perform that action at this time.
0 commit comments