@@ -428,7 +428,6 @@ def roundtrip_truncated(arr):
428
428
arr2 = format .read_array (f2 )
429
429
return arr2
430
430
431
-
432
431
def assert_equal_ (o1 , o2 ):
433
432
assert_ (o1 == o2 )
434
433
@@ -451,6 +450,30 @@ def test_roundtrip_truncated():
451
450
if arr .dtype != object :
452
451
assert_raises (ValueError , roundtrip_truncated , arr )
453
452
453
+ def test_file_truncated (tmp_path ):
454
+ path = tmp_path / "a.npy"
455
+ for arr in basic_arrays :
456
+ if arr .dtype != object :
457
+ with open (path , 'wb' ) as f :
458
+ format .write_array (f , arr )
459
+ #truncate the file by one byte
460
+ with open (path , 'rb+' ) as f :
461
+ f .seek (- 1 , os .SEEK_END )
462
+ f .truncate ()
463
+ with open (path , 'rb' ) as f :
464
+ with pytest .raises (
465
+ ValueError ,
466
+ match = (
467
+ r"EOF: reading array header, "
468
+ r"expected (\d+) bytes got (\d+)"
469
+ ) if arr .size == 0 else (
470
+ r"Failed to read all data for array\. "
471
+ r"Expected \(.*?\) = (\d+) elements, "
472
+ r"could only read (\d+) elements\. "
473
+ r"\(file seems not fully written\?\)"
474
+ )
475
+ ):
476
+ _ = format .read_array (f )
454
477
455
478
def test_long_str ():
456
479
# check items larger than internal buffer size, gh-4027
0 commit comments