|
1 | 1 | import pytest |
2 | 2 | import collections |
3 | 3 | import datetime |
| 4 | +import os |
| 5 | +import tempfile |
4 | 6 | import unittest |
5 | 7 | import warnings |
6 | 8 | from io import BytesIO |
@@ -337,6 +339,105 @@ def test_write_array_to_file_f(self) -> None: |
337 | 339 | fp = BytesIO() |
338 | 340 | write_array_to_file(a1, fp) |
339 | 341 | self.assertEqual(fp.getvalue(), b'') |
| 342 | + |
| 343 | + #--------------------------------------------------------------------------- |
| 344 | + # fd fast-path tests (real files expose fileno(), so these exercise the |
| 345 | + # direct file-descriptor write path rather than the Python write() fallback) |
| 346 | + |
| 347 | + def test_write_array_to_file_fd_a(self) -> None: |
| 348 | + # basic 1d C-contiguous array to a real (buffered) file |
| 349 | + a1 = np.arange(20, dtype=np.int64) |
| 350 | + with tempfile.TemporaryDirectory() as d: |
| 351 | + fp = os.path.join(d, 'a.bin') |
| 352 | + with open(fp, 'wb') as f: |
| 353 | + write_array_to_file(a1, f) |
| 354 | + with open(fp, 'rb') as f: |
| 355 | + self.assertEqual(f.read(), a1.tobytes()) |
| 356 | + |
| 357 | + def test_write_array_to_file_fd_b(self) -> None: |
| 358 | + # 2d array, C order, small buffersize forces multiple chunks |
| 359 | + a1 = np.arange(12).reshape(3, 4) |
| 360 | + with tempfile.TemporaryDirectory() as d: |
| 361 | + fp = os.path.join(d, 'b.bin') |
| 362 | + with open(fp, 'wb') as f: |
| 363 | + write_array_to_file(a1, f, buffersize=2) |
| 364 | + with open(fp, 'rb') as f: |
| 365 | + self.assertEqual(f.read(), a1.tobytes('C')) |
| 366 | + |
| 367 | + def test_write_array_to_file_fd_c(self) -> None: |
| 368 | + # 2d fortran-ordered array written in fortran order |
| 369 | + a1 = np.arange(12).reshape(3, 4).T |
| 370 | + with tempfile.TemporaryDirectory() as d: |
| 371 | + fp = os.path.join(d, 'c.bin') |
| 372 | + with open(fp, 'wb') as f: |
| 373 | + write_array_to_file(a1, f, fortran_order=True, buffersize=2) |
| 374 | + with open(fp, 'rb') as f: |
| 375 | + self.assertEqual(f.read(), a1.tobytes('F')) |
| 376 | + |
| 377 | + def test_write_array_to_file_fd_d(self) -> None: |
| 378 | + # non-contiguous array (strided view) exercises the packing path |
| 379 | + a1 = np.arange(10, dtype=np.int16)[::2] |
| 380 | + with tempfile.TemporaryDirectory() as d: |
| 381 | + fp = os.path.join(d, 'd.bin') |
| 382 | + with open(fp, 'wb') as f: |
| 383 | + write_array_to_file(a1, f, buffersize=3) |
| 384 | + with open(fp, 'rb') as f: |
| 385 | + self.assertEqual(f.read(), a1.tobytes('C')) |
| 386 | + |
| 387 | + def test_write_array_to_file_fd_flush(self) -> None: |
| 388 | + # regression: a Python-level buffered write before the fd write must be |
| 389 | + # preserved. Without an internal flush, the array bytes would be written |
| 390 | + # at the kernel offset ahead of the still-buffered header, corrupting |
| 391 | + # the file. |
| 392 | + a1 = np.arange(8, dtype=np.int64) |
| 393 | + header = b'HEADER-DATA' |
| 394 | + with tempfile.TemporaryDirectory() as d: |
| 395 | + fp = os.path.join(d, 'flush.bin') |
| 396 | + with open(fp, 'wb') as f: |
| 397 | + f.write(header) |
| 398 | + write_array_to_file(a1, f) |
| 399 | + with open(fp, 'rb') as f: |
| 400 | + self.assertEqual(f.read(), header + a1.tobytes()) |
| 401 | + |
| 402 | + def test_write_array_to_file_fd_interleave(self) -> None: |
| 403 | + # multiple array writes interleaved with Python writes stay ordered |
| 404 | + a1 = np.arange(4, dtype=np.int32) |
| 405 | + a2 = np.arange(4, 8, dtype=np.int32) |
| 406 | + with tempfile.TemporaryDirectory() as d: |
| 407 | + fp = os.path.join(d, 'inter.bin') |
| 408 | + with open(fp, 'wb') as f: |
| 409 | + f.write(b'<') |
| 410 | + write_array_to_file(a1, f) |
| 411 | + f.write(b'|') |
| 412 | + write_array_to_file(a2, f) |
| 413 | + f.write(b'>') |
| 414 | + with open(fp, 'rb') as f: |
| 415 | + self.assertEqual( |
| 416 | + f.read(), |
| 417 | + b'<' + a1.tobytes() + b'|' + a2.tobytes() + b'>') |
| 418 | + |
| 419 | + def test_write_array_to_file_fd_raw_fd(self) -> None: |
| 420 | + # an os.open integer fd (no .flush) must still work |
| 421 | + a1 = np.arange(6, dtype=np.float64) |
| 422 | + with tempfile.TemporaryDirectory() as d: |
| 423 | + fp = os.path.join(d, 'raw.bin') |
| 424 | + fd = os.open(fp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) |
| 425 | + try: |
| 426 | + write_array_to_file(a1, fd) |
| 427 | + finally: |
| 428 | + os.close(fd) |
| 429 | + with open(fp, 'rb') as f: |
| 430 | + self.assertEqual(f.read(), a1.tobytes()) |
| 431 | + |
| 432 | + def test_write_array_to_file_fd_empty(self) -> None: |
| 433 | + # zero-size array to a real file produces an empty file |
| 434 | + a1 = np.array([], dtype=np.int64) |
| 435 | + with tempfile.TemporaryDirectory() as d: |
| 436 | + fp = os.path.join(d, 'empty.bin') |
| 437 | + with open(fp, 'wb') as f: |
| 438 | + write_array_to_file(a1, f) |
| 439 | + with open(fp, 'rb') as f: |
| 440 | + self.assertEqual(f.read(), b'') |
340 | 441 | #--------------------------------------------------------------------------- |
341 | 442 | def test_array_to_tuple_array_1d_a(self) -> None: |
342 | 443 | a1 = np.arange(10) |
|
0 commit comments