@@ -14,7 +14,7 @@ from cpython cimport PyBytes_Check, PyUnicode_Check
14
14
from cpython cimport array as c_array
15
15
from libc.errno cimport errno
16
16
from libc.stdlib cimport calloc, free
17
- from libc.string cimport strncpy
17
+ from libc.string cimport strerror, strncpy
18
18
from libc.stdint cimport INT32_MAX, int32_t
19
19
from libc.stdio cimport fprintf, stderr, fflush
20
20
from libc.stdio cimport stdout as c_stdout
@@ -175,6 +175,12 @@ cdef decode_bytes(bytes s, encoding=None, errors=None):
175
175
return s.decode(encoding or TEXT_ENCODING, errors or ERROR_HANDLER)
176
176
177
177
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
+
178
184
cpdef parse_region(contig = None ,
179
185
start = None ,
180
186
stop = None ,
@@ -324,7 +330,7 @@ def _pysam_dispatch(collection,
324
330
stdout_h = c_open(force_bytes(stdout_f),
325
331
O_WRONLY| O_CREAT| O_TRUNC, 0666 )
326
332
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)
328
334
329
335
samtools_set_stdout_fn(force_bytes(stdout_f))
330
336
bcftools_set_stdout_fn(force_bytes(stdout_f))
0 commit comments