@@ -65,13 +65,13 @@ import re
65
65
import warnings
66
66
import array
67
67
from libc.errno cimport errno, EPIPE
68
- from libc.string cimport strcmp, strpbrk, strerror
68
+ from libc.string cimport strcmp, strpbrk
69
69
from libc.stdint cimport INT32_MAX
70
70
71
71
from cpython cimport array as c_array
72
72
73
73
from pysam.libcutils cimport force_bytes, force_str, charptr_to_str
74
- from pysam.libcutils cimport encode_filename, from_string_and_size
74
+ from pysam.libcutils cimport OSError_from_errno, encode_filename, from_string_and_size
75
75
from pysam.libcalignedsegment cimport makeAlignedSegment, makePileupColumn
76
76
from pysam.libchtslib cimport HTSFile, hisremote, sam_index_load2, sam_index_load3, \
77
77
HTS_IDX_SAVE_REMOTE, HTS_IDX_SILENT_FAIL
@@ -818,6 +818,7 @@ cdef class AlignmentFile(HTSFile):
818
818
cdef char * cindexname = NULL
819
819
cdef char * cmode = NULL
820
820
cdef bam_hdr_t * hdr = NULL
821
+ cdef int ret
821
822
822
823
if threads > 1 and ignore_truncation:
823
824
# This won't raise errors if reaching a truncated alignment,
@@ -921,9 +922,7 @@ cdef class AlignmentFile(HTSFile):
921
922
922
923
if self .htsfile == NULL :
923
924
if errno:
924
- raise IOError (errno, " could not open alignment file `{}`: {}" .format(
925
- force_str(filename),
926
- force_str(strerror(errno))))
925
+ raise OSError_from_errno(" Could not open alignment file" , filename)
927
926
else :
928
927
raise ValueError (" could not open alignment file `{}`" .format(force_str(filename)))
929
928
if format_options and len (format_options):
@@ -939,16 +938,17 @@ cdef class AlignmentFile(HTSFile):
939
938
if " b" in mode or " c" in mode or " h" in mode:
940
939
hdr = self .header.ptr
941
940
with nogil:
942
- sam_hdr_write(self .htsfile, hdr)
941
+ ret = sam_hdr_write(self .htsfile, hdr)
942
+ if ret < 0 :
943
+ raise OSError_from_errno(" Could not write headers" , filename)
943
944
944
945
elif mode[0 ] == " r" :
945
946
# open file for reading
946
947
self .htsfile = self ._open_htsfile()
947
948
948
949
if self .htsfile == NULL :
949
950
if errno:
950
- raise IOError (errno, " could not open alignment file `{}`: {}" .format(force_str(filename),
951
- force_str(strerror(errno))))
951
+ raise OSError_from_errno(" Could not open alignment file" , filename)
952
952
else :
953
953
raise ValueError (" could not open alignment file `{}`" .format(force_str(filename)))
954
954
@@ -1014,7 +1014,7 @@ cdef class AlignmentFile(HTSFile):
1014
1014
1015
1015
if not self .index and (cindexname or require_index):
1016
1016
if errno:
1017
- raise IOError (errno, force_str(strerror(errno)) )
1017
+ raise OSError_from_errno( " Could not open index file " , self .index_filename )
1018
1018
else :
1019
1019
raise IOError (' unable to open index file `%s `' % self .index_filename)
1020
1020
@@ -1679,12 +1679,11 @@ cdef class AlignmentFile(HTSFile):
1679
1679
1680
1680
self .header = None
1681
1681
1682
- if ret < 0 :
1683
- global errno
1684
- if errno == EPIPE:
1685
- errno = 0
1682
+ if ret < 0 and errno != EPIPE:
1683
+ if isinstance (self .filename, (str , bytes)):
1684
+ raise OSError_from_errno(" Closing failed" , self .filename)
1686
1685
else :
1687
- raise IOError (errno, force_str(strerror(errno)) )
1686
+ raise OSError_from_errno( " Closing failed " )
1688
1687
1689
1688
def __dealloc__ (self ):
1690
1689
cdef int ret = 0
@@ -1703,12 +1702,11 @@ cdef class AlignmentFile(HTSFile):
1703
1702
bam_destroy1(self .b)
1704
1703
self .b = NULL
1705
1704
1706
- if ret < 0 :
1707
- global errno
1708
- if errno == EPIPE:
1709
- errno = 0
1705
+ if ret < 0 and errno != EPIPE:
1706
+ if isinstance (self .filename, (str , bytes)):
1707
+ raise OSError_from_errno(" Closing failed" , self .filename)
1710
1708
else :
1711
- raise IOError (errno, force_str(strerror(errno)) )
1709
+ raise OSError_from_errno( " Closing failed " )
1712
1710
1713
1711
cpdef int write(self , AlignedSegment read) except - 1 :
1714
1712
'''
@@ -1742,8 +1740,7 @@ cdef class AlignmentFile(HTSFile):
1742
1740
# when ret == -1 we get a "SystemError: error return without
1743
1741
# exception set".
1744
1742
if ret < 0 :
1745
- raise IOError (
1746
- " sam_write1 failed with error code {}" .format(ret))
1743
+ raise IOError (" sam_write1 failed with error code {}" .format(ret))
1747
1744
1748
1745
return ret
1749
1746
@@ -2298,14 +2295,14 @@ cdef class IteratorRowSelection(IteratorRow):
2298
2295
# end iteration if out of positions
2299
2296
if self .current_pos >= len (self .positions): return - 1
2300
2297
2298
+ cdef int ret
2301
2299
cdef uint64_t pos = self .positions[self .current_pos]
2302
2300
with nogil:
2303
- bgzf_seek(hts_get_bgzfp(self .htsfile),
2304
- pos,
2305
- 0 )
2301
+ ret = bgzf_seek(hts_get_bgzfp(self .htsfile), pos, 0 )
2302
+ if ret < 0 :
2303
+ raise OSError_from_errno( " Can't seek " , self .samfile.filename )
2306
2304
self .current_pos += 1
2307
2305
2308
- cdef int ret
2309
2306
cdef bam_hdr_t * hdr = self .header.ptr
2310
2307
with nogil:
2311
2308
ret = sam_read1(self .htsfile,
@@ -2911,7 +2908,7 @@ cdef class IndexedReads:
2911
2908
with nogil:
2912
2909
self .htsfile = hts_open(cfilename, ' r' )
2913
2910
if self .htsfile == NULL :
2914
- raise OSError ( " unable to reopen htsfile " )
2911
+ raise OSError_from_errno( " Unable to reopen file " , cfilename )
2915
2912
2916
2913
# need to advance in newly opened file to position after header
2917
2914
# better: use seek/tell?
0 commit comments