8000 Add OSError_from_errno() utility function · pysam-developers/pysam@bc19635 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc19635

Browse files
committed
Add OSError_from_errno() utility function
1 parent 0024d81 commit bc19635

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

pysam/libcutils.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cpdef parse_region(contig=*, start=*, stop=*, region=*, reference=*, end=*)
99

1010
cdef int libc_whence_from_io(int whence)
1111

12+
cdef OSError_from_errno(message, filename=*)
13+
1214
#########################################################################
1315
# Utility functions for quality string conversions
1416

pysam/libcutils.pyx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from cpython cimport PyBytes_Check, PyUnicode_Check
1414
from cpython cimport array as c_array
1515
from libc.errno cimport errno
1616
from libc.stdlib cimport calloc, free
17-
from libc.string cimport strncpy
17+
from libc.string cimport strerror, strncpy
1818
from libc.stdint cimport INT32_MAX, int32_t
1919
from libc.stdio cimport fprintf, stderr, fflush
2020
from libc.stdio cimport stdout as c_stdout
@@ -175,6 +175,12 @@ cdef decode_bytes(bytes s, encoding=None, errors=None):
175175
return s.decode(encoding or TEXT_ENCODING, errors or ERROR_HANDLER)
176176

177177

178+
cdef OSError_from_errno(message, filename=None):
179+
cdef int err = errno
180+
if filename is not None: filename = os.fsdecode(filename)
181+
return OSError(err, f"{message}: {strerror(err).decode()}", filename)
182+
183+
178184
cpdef parse_region(contig=None,
179185
start=None,
180186
stop=None,
@@ -324,7 +330,7 @@ def _pysam_dispatch(collection,
324330
stdout_h = c_open(force_bytes(stdout_f),
325331
O_WRONLY|O_CREAT|O_TRUNC, 0666)
326332
if stdout_h == -1:
327-
raise OSError(errno, "error while opening file for writing", stdout_f)
333+
raise OSError_from_errno("Could not redirect standard output", stdout_f)
328334

329335
samtools_set_stdout_fn(force_bytes(stdout_f))
330336
bcftools_set_stdout_fn(force_bytes(stdout_f))

0 commit comments

Comments
 (0)
0