Skip to content

Commit ea821ac

Browse files
committed
fix numpy 2.0 int32 scalars to_bytes() error
- in NumPy 2.0, int32 scalars to_bytes() fails with 'AttributeError: 'numpy.int32' object has no attribute 'to_bytes'' - this is because of stricter handling of types in Numpy 2.0 than NumPy 1.x - cast numpy.int32 to a native Python int before calling to_bytes()
1 parent c6cddec commit ea821ac

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

surfa/io/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def write_int(file, value, size=4, signed=True, byteorder='big'):
6464
byteorder : str
6565
Memory byte order.
6666
"""
67-
file.write(value.to_bytes(size, byteorder=byteorder, signed=signed))
67+
# to avoid 'AttributeError: 'numpy.int32' object has no attribute 'to_bytes'' in numpy 2.0,
68+
# cast numpy.int32 to a native Python int
69+
file.write(int(value).to_bytes(size, byteorder=byteorder, signed=signed))
6870

6971

7072
def read_bytes(file, dtype, count=1):

0 commit comments

Comments
 (0)