Skip to content

Commit

Permalink
Fix PysamDispatch(save_stdout=filename) file opening
Browse files Browse the repository at this point in the history
Opening the save_stdout filename for writing needs to create
the file in the likely case it doesn't already exist. Fixes #677.
  • Loading branch information
jmarshall committed Oct 8, 2024
1 parent 587bff2 commit 635c667
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pysam/libcutils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ from codecs import register_error
from cpython.version cimport PY_MAJOR_VERSION, PY_MINOR_VERSION
from cpython cimport PyBytes_Check, PyUnicode_Check
from cpython cimport array as c_array
from libc.errno cimport errno
from libc.stdlib cimport calloc, free
from libc.string cimport strncpy
from libc.stdint cimport INT32_MAX, int32_t
from libc.stdio cimport fprintf, stderr, fflush
from libc.stdio cimport stdout as c_stdout
from posix.fcntl cimport open as c_open, O_WRONLY
from posix.fcntl cimport open as c_open, O_WRONLY, O_CREAT, O_TRUNC
from posix.unistd cimport SEEK_SET, SEEK_CUR, SEEK_END

from pysam.libcsamtools cimport samtools_dispatch, samtools_set_stdout, samtools_set_stderr, \
Expand Down Expand Up @@ -321,9 +322,9 @@ def _pysam_dispatch(collection,
if save_stdout:
stdout_f = save_stdout
stdout_h = c_open(force_bytes(stdout_f),
O_WRONLY)
O_WRONLY|O_CREAT|O_TRUNC, 0666)
if stdout_h == -1:
raise IOError("error while opening {} for writing".format(stdout_f))
raise OSError(errno, "error while opening file for writing", stdout_f)

samtools_set_stdout_fn(force_bytes(stdout_f))
bcftools_set_stdout_fn(force_bytes(stdout_f))
Expand Down

0 comments on commit 635c667

Please sign in to comment.