pysam-developers/pysam — pull request #50 by cmclean (2014-10-30 20:44:26+00:00)
Title: Raise ValueError when iteration hits truncated line rather than silently exiting

All of the iterators that call sam_read1 currently raise StopIteration for any ret < 0. This leads to the undesirable behavior that iterating through a truncated file using pysam silently just stops when the truncated line occurs.

This pull request causes truncated lines to raise ValueError to distinguish between EOF and truncated lines. The magic -2 value comes from pysam/htslib/sam.c: line 877 sets the return value to the value of bam_read1(..) for binary data, which returns -2 for truncated data (line 331 of pysam/htslib/sam.c).