Skip to content

Commit

Permalink
Add HTSFile.flush() for flushing output streams
Browse files Browse the repository at this point in the history
Enables programs to explicitly flush AlignmentFile and
VariantFile objects. Fixes #1299.
  • Loading branch information
jmarshall committed Oct 10, 2024
1 parent 453acaa commit c66d85d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pysam/libchtslib.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,12 @@ cdef extern from "htslib/hts.h" nogil:
# @param mode Open mode, as per hts_open()
htsFile *hts_hopen(hFILE *fp, const char *fn, const char *mode)

# @abstract For output streams, flush any buffered data
# @param fp The file handle to be flushed
# @return 0 for success, or negative if an error occurred.
# @since 1.14
int hts_flush(htsFile *fp)

# @abstract Close a file handle, flushing buffered data for output streams
# @param fp The file handle to be closed
# @return 0 for success, or negative if an error occurred.
Expand Down
1 change: 1 addition & 0 deletions pysam/libchtslib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class HTSFile:
@property
def duplicate_filehandle(self) -> bool: ...
def close(self) -> None: ...
def flush(self) -> None: ...
def check_truncation(self, ignore_truncation: bool = ...) -> None: ...
@property
def category(self) -> str: ...
Expand Down
6 changes: 6 additions & 0 deletions pysam/libchtslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ cdef class HTSFile(object):
hts_close(self.htsfile)
self.htsfile = NULL

def flush(self):
"""Flush any buffered data to the underlying output stream."""
if self.htsfile:
if hts_flush(self.htsfile) < 0:
raise OSError(errno, f'Flushing {type(self).__name__} failed', force_str(self.filename))

def check_truncation(self, ignore_truncation=False):
"""Check if file is truncated."""
if not self.htsfile:
Expand Down

0 comments on commit c66d85d

Please sign in to comment.